Isti <> 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
|