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

Discussion in 'C++' started by Isti, Apr 19, 2010.

  1. Isti

    Isti Guest

    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
     
    Isti, Apr 19, 2010
    #1
    1. Advertising

  2. Re: decltype as a template parameter when containing reference to another template parameter.

    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
     
    Paul Bibbings, Apr 19, 2010
    #2
    1. Advertising

  3. Isti

    Isti Guest

    Re: decltype as a template parameter when containing reference toanother template parameter.


    > 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
     
    Isti, Apr 19, 2010
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    5
    Views:
    622
  2. Replies:
    5
    Views:
    669
  3. Noah Roberts

    decltype and const reference params

    Noah Roberts, Apr 7, 2010, in forum: C++
    Replies:
    7
    Views:
    1,021
    Joe Gottman
    Apr 8, 2010
  4. gnobal
    Replies:
    5
    Views:
    755
    gnobal
    Jul 4, 2011
  5. Noah Roberts

    decltype in template param list?

    Noah Roberts, Jul 22, 2011, in forum: C++
    Replies:
    4
    Views:
    1,037
    Victor Bazarov
    Jul 26, 2011
Loading...

Share This Page