problem with template template parameter.

R

red floyd

I'm playing with template template parameters. I'm not
sure what I'm doing wrong here. Could someone with better
template template experience give me a hand?

Code and Comeau online error below.


#include <vector>

template<template<typename> class C, typename T>
T* iterator_cast(typename C<T>::iterator it)
{
return &*it;
}

int* cast_tester(std::vector<int>& c)
{
return iterator_cast(c.begin()); // line 12
}


Comeau online gives me the following error (G++ 3.4.4 gives a similar errr):

"ComeauTest.c", line 12: error: no instance of function template
"iterator_cast"
matches the argument list
The argument types that you used are: (std::vector<int,
std::allocator<int>>::iterator)
return iterator_cast(c.begin());
 
V

Victor Bazarov

red floyd said:
I'm playing with template template parameters. I'm not
sure what I'm doing wrong here. Could someone with better
template template experience give me a hand?

Code and Comeau online error below.


#include <vector>

template<template<typename> class C, typename T>
T* iterator_cast(typename C<T>::iterator it)
{
return &*it;
}

int* cast_tester(std::vector<int>& c)
{
return iterator_cast(c.begin()); // line 12
}


Comeau online gives me the following error (G++ 3.4.4 gives a similar
errr):

"ComeauTest.c", line 12: error: no instance of function template
"iterator_cast"
matches the argument list
The argument types that you used are: (std::vector<int,
std::allocator<int>>::iterator)
return iterator_cast(c.begin());

Most likely you've run into non-deducible context. I don't think
that 'T' can be deduced from a member of C<T>.

V
 
R

red floyd

Victor said:
Most likely you've run into non-deducible context. I don't think
that 'T' can be deduced from a member of C<T>.

By the way, the rationale behind this function is to make it obvious in
the code when we are converting an iterator to a pointer.


Given the potential non-deducible context, , I've gotten rid of the
template template, and went with a "simple" template.

#include <vector>
#include <iterator>

template <typename Iterator>
typename std::iterator_traits<Iterator>::pointer_type
iterator_cast(Iterator iter)
{
return &*iter;
}


int* cast_tester(std::vector<int>& c)
{
return iterator_cast(c.begin());
}


Comeau online still errors:
Comeau C/C++ 4.3.3 (Aug 6 2003 15:13:37) for ONLINE_EVALUATION_BETA1
Copyright 1988-2003 Comeau Computing. All rights reserved.
MODE:strict errors C++

"ComeauTest.c", line 13: error: no instance of function template
"iterator_cast"
matches the argument list
The argument types that you used are: (std::vector<int,
std::allocator<int>>::iterator)
return iterator_cast(c.begin());
^

1 error detected in the compilation of "ComeauTest.c".
 
R

red floyd

red said:
By the way, the rationale behind this function is to make it obvious in
the code when we are converting an iterator to a pointer.


Given the potential non-deducible context, , I've gotten rid of the
template template, and went with a "simple" template.

#include <vector>
#include <iterator>

template <typename Iterator>
typename std::iterator_traits<Iterator>::pointer_type
iterator_cast(Iterator iter)
{
return &*iter;
}


int* cast_tester(std::vector<int>& c)
{
return iterator_cast(c.begin());
}


Comeau online still errors:
Comeau C/C++ 4.3.3 (Aug 6 2003 15:13:37) for ONLINE_EVALUATION_BETA1
Copyright 1988-2003 Comeau Computing. All rights reserved.
MODE:strict errors C++

"ComeauTest.c", line 13: error: no instance of function template
"iterator_cast"
matches the argument list
The argument types that you used are: (std::vector<int,
std::allocator<int>>::iterator)
return iterator_cast(c.begin());
^

1 error detected in the compilation of "ComeauTest.c".

Never mind. Found the error. I shouldn't have used "pointer_type", but
only "pointer" as the return type.
 
V

Victor Bazarov

red floyd said:
[..]
Given the potential non-deducible context, , I've gotten rid of the
template template, and went with a "simple" template.

#include <vector>
#include <iterator>

template <typename Iterator>
typename std::iterator_traits<Iterator>::pointer_type
iterator_cast(Iterator iter)
{
return &*iter;
}


int* cast_tester(std::vector<int>& c)
{
return iterator_cast(c.begin());
}


Comeau online still errors:
Comeau C/C++ 4.3.3 (Aug 6 2003 15:13:37) for ONLINE_EVALUATION_BETA1
Copyright 1988-2003 Comeau Computing. All rights reserved.
MODE:strict errors C++

"ComeauTest.c", line 13: error: no instance of function template
"iterator_cast"
matches the argument list
The argument types that you used are: (std::vector<int,
std::allocator<int>>::iterator)
return iterator_cast(c.begin());
^

1 error detected in the compilation of "ComeauTest.c".

Are you sure that 'pointer_type' is actually defined for the iterator_traits
instantiated on the vector's iterator?

It seems that you're running into SFINAE -- if the function cannot be
created
(instantiated) becuase of non-existent return value type, no function exists
to be found in the 'return' expression.

V

P.S. Sorry for overquoting, everybody.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,016
Latest member
TatianaCha

Latest Threads

Top