what happened to my sfinae?

Discussion in 'C++' started by Belebele, Feb 14, 2011.

  1. Belebele

    Belebele Guest

    in t.cpp:

    #include <boost/type_traits.hpp>
    #include <boost/utility.hpp>

    template <typename T>
    struct Foo {
    typename boost::enable_if<boost::is_pointer<T>, void>::type
    bar(T ) const {
    }

    typename boost::disable_if<boost::is_pointer<T>, void>::type
    bar(T ) const {
    }
    };


    void call_bar()
    {
    Foo<int>().bar(5);
    }

    // end

    $ g++ -c -I/my/path/to/boost t.cpp
    t.cpp: In instantiation of 'Foo<int>':
    t.cpp:18: instantiated from here
    t.cpp:7: error: no type named 'type' in 'struct
    boost::enable_if<boost::is_pointer ....

    $ g++ --v
    gcc version 4.1.2

    What happen to my sfinae?

    Thanks
    Belebele, Feb 14, 2011
    #1
    1. Advertising

  2. Belebele

    Belebele Guest

    ah, I figured it out. SFINAE works only on function templates.
    Belebele, Feb 14, 2011
    #2
    1. Advertising

  3. * Belebele, on 14.02.2011 23:25:
    > in t.cpp:
    >
    > #include<boost/type_traits.hpp>
    > #include<boost/utility.hpp>
    >
    > template<typename T>
    > struct Foo {
    > typename boost::enable_if<boost::is_pointer<T>, void>::type
    > bar(T ) const {
    > }
    >
    > typename boost::disable_if<boost::is_pointer<T>, void>::type
    > bar(T ) const {
    > }
    > };
    >
    >
    > void call_bar()
    > {
    > Foo<int>().bar(5);
    > }
    >
    > // end
    >
    > $ g++ -c -I/my/path/to/boost t.cpp
    > t.cpp: In instantiation of 'Foo<int>':
    > t.cpp:18: instantiated from here
    > t.cpp:7: error: no type named 'type' in 'struct
    > boost::enable_if<boost::is_pointer ....
    >
    > $ g++ --v
    > gcc version 4.1.2
    >
    > What happen to my sfinae?


    It wasn't there in the first place.

    Try


    #include <boost/type_traits.hpp>
    #include <boost/utility.hpp>

    struct Foo {
    template <typename T>
    typename boost::enable_if<boost::is_pointer<T>, void>::type
    bar(T ) const {
    }

    template <typename T>
    typename boost::disable_if<boost::is_pointer<T>, void>::type
    bar(T ) const {
    }
    };


    void call_bar()
    {
    Foo().bar<int>(5);
    }

    // end



    Cheers & hth.,

    - Alf

    --
    blog at <url: http://alfps.wordpress.com>
    Alf P. Steinbach /Usenet, Feb 15, 2011
    #3
  4. Belebele

    Marc Guest

    "Alf P. Steinbach /Usenet" wrote:

    > * Belebele, on 14.02.2011 23:25:
    >> in t.cpp:
    >>
    >> #include<boost/type_traits.hpp>
    >> #include<boost/utility.hpp>
    >>
    >> template<typename T>
    >> struct Foo {
    >> typename boost::enable_if<boost::is_pointer<T>, void>::type
    >> bar(T ) const {
    >> }
    >>
    >> typename boost::disable_if<boost::is_pointer<T>, void>::type
    >> bar(T ) const {
    >> }
    >> };
    >>
    >>
    >> void call_bar()
    >> {
    >> Foo<int>().bar(5);
    >> }
    >>
    >> // end
    >>
    >> $ g++ -c -I/my/path/to/boost t.cpp
    >> t.cpp: In instantiation of 'Foo<int>':
    >> t.cpp:18: instantiated from here
    >> t.cpp:7: error: no type named 'type' in 'struct
    >> boost::enable_if<boost::is_pointer ....
    >>
    >> $ g++ --v
    >> gcc version 4.1.2
    >>
    >> What happen to my sfinae?

    >
    > It wasn't there in the first place.


    However, I regularly wish it was. There are a number of things that
    are doable but painful with templates. Applying sfinae more widely
    would help some. static_if (somewhere between #if and if) would also
    be very convenient. Assuming they can be given sensible semantics of
    course.
    Marc, Feb 15, 2011
    #4
    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. Peter Collingbourne

    problem with SFINAE applied to class methods

    Peter Collingbourne, Jul 1, 2004, in forum: C++
    Replies:
    8
    Views:
    448
    Pete Becker
    Jul 4, 2004
  2. Replies:
    3
    Views:
    1,653
    Attila Feher
    Feb 8, 2005
  3. Clark S. Cox III

    Using SFINAE with constructors

    Clark S. Cox III, Sep 9, 2005, in forum: C++
    Replies:
    2
    Views:
    368
    Howard Hinnant
    Sep 9, 2005
  4. christopher diggins

    SFINAE problem.

    christopher diggins, Sep 26, 2005, in forum: C++
    Replies:
    4
    Views:
    447
    christopher diggins
    Sep 26, 2005
  5. SFINAE

    , Apr 26, 2006, in forum: C++
    Replies:
    4
    Views:
    462
    n2xssvv g02gfr12930
    Apr 26, 2006
Loading...

Share This Page