Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Problem compiling template function with typename for dependent names

Reply
Thread Tools

Problem compiling template function with typename for dependent names

 
 
antoniogarcar@gmail.com
Guest
Posts: n/a
 
      11-20-2012
Hi all,

I have a short question. The following program does not compile using g++4.6.3, ¿why?


#include <list>

template <class T>
void empty (typename std::list<T>::iterator it)
{}

int main()
{
std::list<int> l;
empty(l.begin());
}
 
Reply With Quote
 
 
 
 
SG
Guest
Posts: n/a
 
      11-20-2012
Am 20.11.2012 09:33, schrieb :
>
> The following program does not compile using g++4.6.3, ¿why?
>
> #include <list>
>
> template <class T>
> void empty (typename std::list<T>::iterator it)


- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

This is a non-deducible context. Or, as Stephan T. Lavavej would say
"The double colon between T and 'iterator' prevents the compiler from
deducing T" (or something like that).

> {}
>
> int main()
> {
> std::list<int> l;
> empty(l.begin());
> }


Here, the compiler won't deduce the template parameter T as a general
rule. You can't expect the compiler to try every possible T and see what
fits. There are other cases where there is no "solution" for T:

template<class T>
struct foo {
typedef int blah;
}

template<class T>
void empty(typename foo<T>::blah);

int main()
{
empty(42); // What's T ?!
}

HTH,
SG

 
Reply With Quote
 
 
 
 
Antonio Garrido
Guest
Posts: n/a
 
      11-20-2012

> HTH,
>
> SG


Thank you. That helps.
 
Reply With Quote
 
Zhihao Yuan
Guest
Posts: n/a
 
      11-20-2012
On Tuesday, November 20, 2012 2:33:41 AM UTC-6, Antonio Garrido wrote:
> template <class T>
> void empty (typename std::list<T>::iterator it)
> {}


First, like SG said, a typename is not deducible. Second, operations
work on a iterator should be generalized regarding to the *type* of
the iterator, not the type of the value. Try this:

template <typename It>
void empty (It it, typename std::enable_if<
std::is_convertible<
typename std::iterator_traits<It>::iterator_category,
std::bidirectional_iterator_tag>::value>::type* = 0) {
// or forward_iterator_tag, etc.
}
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
non-dependent vs. dependent template names puzzlecracker C++ 1 08-07-2008 07:42 AM
Compiler error dependent name is not a type prefix with 'typename' toindicate a type Sanil C++ 2 12-14-2007 09:14 AM
Question about dependent names and typename Evan C++ 3 11-28-2006 09:37 PM
Q: typename or not typename? Jakob Bieling C++ 2 03-14-2006 03:44 PM
xlc - A template dependent name that is a type must be qualified with "typename" pervinder C++ 4 03-29-2005 04:40 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57