Me or compiler? 0x

N

Noah Roberts

Input:

template < typename T >
boost::function<void()> f()
{
return []() { typedef typename T::type type; };
}

Output: "typename cannot be used outside a template declaration",
"'T' : is not a class or namespace name"

Input:

template < typename T >
boost::function<void()> f()
{
return []() { typedef T::type type; };
}

Output: "'T' : is not a class or namespace name"

Input:

template < typename T >
struct wtf { typedef typename T::type type; };

template < typename T >
boost::function<void()> f()
{
return []() { typedef typename wtf<T>::type type; };
}

Output: "typename cannot be used outside a template declaration"

Input:

template < typename T >
struct wtf { typedef typename T::type type; };

template < typename T >
boost::function<void()> f()
{
return []() { typedef wtf<T>::type type; };
}

Output: success (assuming T has a type in it)

So the question is, am I doing something wrong or is the compiler just
confused? What of these is correct?
 
S

SG

Input:

template < typename T >
boost::function<void()> f()
{
  return []() { typedef typename T::type type; };
}

Output:  "typename cannot be used outside a template declaration",
"'T' : is not a class or namespace name"

I also would have expected it to work. G++ 4.5.1 compiles the
following code:

template<class T>
void f() {
[]() { typedef typename T::type type; }();
}

struct foo {
typedef int type;
};

int main() {
f<foo>();
}

So, I guess that (1) you're using the Microsoft Compiler and (2) you
encountered a compiler bug.

Have you tried the following?

template < typename T >
boost::function<void()> f()
{
typedef typename T::type T_type;
return []() { typedef T_type type; };
}


Cheers!
SG
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top