Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Me or compiler? 0x

Reply
Thread Tools

Me or compiler? 0x

 
 
Noah Roberts
Guest
Posts: n/a
 
      01-03-2011

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?


--
http://crazyeddiecpp.blogspot.com/
 
Reply With Quote
 
 
 
 
SG
Guest
Posts: n/a
 
      01-04-2011
On 4 Jan., 00:45, Noah Roberts wrote:
> 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
 
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




Advertisments