Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > decltype as a template parameter when containing reference to anothertemplate parameter.

Reply
Thread Tools

decltype as a template parameter when containing reference to anothertemplate parameter.

 
 
Isti
Guest
Posts: n/a
 
      04-19-2010
consider the following code:
--------------------------------
#include <iostream>

template<class T> struct always_void { typedef void type; };

template<class T, class Enable = void>
struct has_foo : public std::false_type {};

template<class T>
struct has_foo<T, typename always_void<decltype(&T::foo)>::type>
: public std::true_type {};

struct F {
void foo() {}
};

int main(int argc, char* argv[])
{
std::cout << has_foo<F>::value << std::endl;
std::cout << has_foo<int>::value << std::endl;
return 0;
}

----------------------------------------------------
I expect has_foo<F>::value to be true when F::foo is well-formed and
false otherwise, but when I compiled this with VS 2010 has_foo<F>
turned out to be always false.
When I changed decltype(&T::foo) to decltype(1): has_foo<T> became
always true (independently from T), as expected, so the basic concept
seems to work.

Is it a bug of VS 2010 or am I wrong somewhere?

Thx in advance:
Istvan Kispal
 
Reply With Quote
 
 
 
 
Paul Bibbings
Guest
Posts: n/a
 
      04-19-2010
Isti <(E-Mail Removed)> writes:

> consider the following code:
> --------------------------------
> #include <iostream>
>
> template<class T> struct always_void { typedef void type; };
>
> template<class T, class Enable = void>
> struct has_foo : public std::false_type {};
>
> template<class T>
> struct has_foo<T, typename always_void<decltype(&T::foo)>::type>
> : public std::true_type {};
>
> struct F {
> void foo() {}
> };
>
> int main(int argc, char* argv[])
> {
> std::cout << has_foo<F>::value << std::endl;
> std::cout << has_foo<int>::value << std::endl;
> return 0;
> }
>
> ----------------------------------------------------
> I expect has_foo<F>::value to be true when F::foo is well-formed and
> false otherwise, but when I compiled this with VS 2010 has_foo<F>
> turned out to be always false.
> When I changed decltype(&T::foo) to decltype(1): has_foo<T> became
> always true (independently from T), as expected, so the basic concept
> seems to work.
>
> Is it a bug of VS 2010 or am I wrong somewhere?
>
> Thx in advance:
> Istvan Kispal


For comparison, using gcc-4.5.0 and taking your code as is:

22:17:48 Paul Bibbings@JIJOU
/cygdrive/d/CPPProjects/nano $gcc -Wall -ansi -pedantic -std=c++0x -o
has_foo.o -c has_foo.cpp

22:18:22 Paul Bibbings@JIJOU
/cygdrive/d/CPPProjects/nano $g++ -o has_foo has_foo.o

22:18:34 Paul Bibbings@JIJOU
/cygdrive/d/CPPProjects/nano $./has_foo
1
0

Regards

Paul Bibbings



 
Reply With Quote
 
 
 
 
Isti
Guest
Posts: n/a
 
      04-19-2010

> For comparison, using gcc-4.5.0 and taking your code as is:
> * */cygdrive/d/CPPProjects/nano $./has_foo
> * *1
> * *0


Thanks Paul!
Now the only remaining question is:
is it a non-standard addition to gcc, or VS 2010 is not fully standard
conformant (considering the current state of C++0x as a standard)?
Does anybody know?

Istvan
 
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
decltype in template param list? Noah Roberts C++ 4 07-26-2011 08:04 PM
decltype to extract function parameter type prior to calling it gnobal C++ 5 07-04-2011 04:44 AM
decltype and const reference params Noah Roberts C++ 7 04-08-2010 12:30 AM
Explicit specialization of member template when type is anothertemplate type. jason.cipriani@gmail.com C++ 5 12-05-2008 05:33 PM
how to partially specialize a template class nested inside anothertemplate class? huili80@gmail.com C++ 5 06-22-2008 01:47 PM



Advertisments