Re: [eigen] C++ Syntax Question Regarding Multiple Template<...>

[ Thread Index | Date Index | More lists.tuxfamily.org/eigen Archives ]



Or is
template<typename InputIterators> void SparseMatrix<double>::setFromTriplets... enough?

Clang 3.4 emits an error without the template<> 
nestedtemplate.cpp:17:1: error: template parameter list matching the non-templated nested type 'A<int>' should be empty ('template<>')

GCC 4.9 error message is a bit more unclear to me :
nestedtemplate.cpp:18:18: error: ambiguating new declaration of 'void A<int>::foo()'
 void A<int>::foo() {
                  ^
nestedtemplate.cpp:12:6: note: old declaration 'void A<T>::foo() [with U = U; T = int]'
 void A<T>::foo() {


On Wed, Mar 26, 2014 at 12:30 AM, Cowing-Zitron, Christopher <ccowingzitron@xxxxxxxx> wrote:
Hello,

I have a technical C++ syntax question that I've asked basically every serious C++ programmer I've ever met, and I've never gotten an answer. The question arose initially while I was reading some Boost code, and it never occurred to me to ask here. But I saw the same pattern in Eigen code today, and since you folks are about as technical C++ as it gets, I thought I'd ask you.

In the header "Eigen/SparseCore/SparseMatrix.h", there is the declaration of the function SparseMatrix::setFromTriplets in lines 433-434 inside the class definition, but the definition of the function is in lines 1008-1013 outside of the class definition. Now, Eigen::SparseMatrix is templated as <typename Scalar, int _Options, typename _Index>, and this member function is a nested template on <typename InputIterators>. I understand the declaration of the nested template just fine (I think, I hope). My question(s) regard the definition, in which there are two template-bracket constructs, one immediately after the other, neither specialized, the first of which contains the template parameters for SparseMatrix and the second containing those for the nested template function. I've pasted both below.

433  template<typename InputIterators>
434  void setFromTriplets(const InputIterators& begin, const InputIterators& end);

-------------------------------------------------

1008 template<typename Scalar, int _Options, typename _Index>
1009 template<typename InputIterators>
1010 void SparseMatrix<Scalar,_Options,_Index>::setFromTriplets(const InputIterators& begin, const InputIterators& end)
1011 {
1012  internal::set_from_triplets(begin, end, *this);
1013 }

My questions are as follows:

1) Is it necessary to separate the two "template bracket" constructs in the definition? Is this required by the standard and/or is it simply necessary to help compilers figure it out? Would it be valid to put everything in one "template bracket", i.e.

template<typename Scalar, int _Options, typename _Index, typename InputIterators>?

Inside the template, would the order matter?

2) My guess is this issue only arises when there is (1) a nested template member of a templated type AND (2) the nested template is declared inside the definition of the parent but defined later, outside of its parent's definition. Is this correct? Is there any other circumstance under which one may/must iterate multiple template-bracket constructs?

3) If either the parent template type (in this case SparseMatrix) or the child template (setFromTriplets) were fully specialized, would it then be necessary to have an empty template-bracket construct to represent the missing (i.e. specified) types, as in

template<> template<typename InputIterators> void SparseMatrix<double>::setFromTriplets...

Or is

template<typename InputIterators> void SparseMatrix<double>::setFromTriplets... enough?

Is the order restricted; that is, if as in that last example, the parent is fully specified but the nested template is not, would it be valid to write

template<typename InputIterators> template<> void SparseMatrix<double>::setFromTriplets...?



I've honestly been asking this question for about 5 years now and never gotten an answer, but I've also never encountered a situation in which I needed to write such a construct and it failed. Thanks in advance to the community for your help.

-- Chris




Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/