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

I

Isti

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
 
P

Paul Bibbings

Isti said:
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
 
I

Isti

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
 

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,961
Messages
2,570,130
Members
46,689
Latest member
liammiller

Latest Threads

Top