SFINAE problem.

Discussion in 'C++' started by christopher diggins, Sep 26, 2005.

  1. I am trying to make compile time decisions based on whether a struct defines
    a member type. It seems to be a job for SFNIAE but I am struggling:

    #include <iostream>
    #include <cstdlib>

    using namespace std;

    struct fu { };
    struct bar { typedef int type; };

    template<typename T, typename U = void>
    struct is_true {
    static const bool value = false;
    };

    template<typename T>
    struct is_true<T, typename T::type> {
    static const bool value = true;
    };

    int main() {
    bool b;
    b = is_true<fu>::value;
    cout << b << endl;
    b = is_true<bar>::value;
    cout << b << endl;
    system("pause");
    };

    However, this outputs:
    0
    0

    I am looking at the following:
    http://boost-consulting.com/boost/libs/utility/enable_if.html

    But it doesn't seem to do what it is that I specifically need.
    Any help would be greatly appreciated. TIA!

    --
    Christopher Diggins
    http://www.cdiggins.com
    christopher diggins, Sep 26, 2005
    #1
    1. Advertising

  2. In article <0hVZe.23741$>,
    "christopher diggins" <> wrote:

    > I am trying to make compile time decisions based on whether a struct defines
    > a member type. It seems to be a job for SFNIAE but I am struggling:
    >
    > #include <iostream>
    > #include <cstdlib>
    >
    > using namespace std;
    >
    > struct fu { };
    > struct bar { typedef int type; };
    >
    > template<typename T, typename U = void>
    > struct is_true {
    > static const bool value = false;
    > };
    >
    > template<typename T>
    > struct is_true<T, typename T::type> {
    > static const bool value = true;
    > };
    >
    > int main() {
    > bool b;
    > b = is_true<fu>::value;
    > cout << b << endl;
    > b = is_true<bar>::value;
    > cout << b << endl;
    > system("pause");
    > };
    >
    > However, this outputs:
    > 0
    > 0


    Your code prints:

    0
    1

    for me (CodeWarrior Pro 10 (beta)).

    -Howard
    Howard Hinnant, Sep 26, 2005
    #2
    1. Advertising

  3. >> However, this outputs:
    >> 0
    >> 0

    >
    > Your code prints:
    >
    > 0
    > 1
    >
    > for me (CodeWarrior Pro 10 (beta)).



    Thanks for checking that out. So I am not entirely a bumbling idiot ;-)
    I am using GCC 3.4.2. So, in theory is what I am doing here correct?

    --
    Christopher Diggins
    http://www.cdiggins.com
    christopher diggins, Sep 26, 2005
    #3
  4. In article <TXVZe.23745$>,
    "christopher diggins" <> wrote:

    > >> However, this outputs:
    > >> 0
    > >> 0

    > >
    > > Your code prints:
    > >
    > > 0
    > > 1
    > >
    > > for me (CodeWarrior Pro 10 (beta)).

    >
    >
    > Thanks for checking that out. So I am not entirely a bumbling idiot ;-)
    > I am using GCC 3.4.2. So, in theory is what I am doing here correct?


    Well I was about to say yes, but now I'm not so sure. I converted your
    example to a compile time test and ran it on Comeau (thanks Greg!):

    struct fu { };
    struct bar { typedef int type; };

    template<typename T, typename U = void>
    struct is_true {
    static const bool value = false;
    };

    template<typename T>
    struct is_true<T, typename T::type> {
    static const bool value = true;
    };

    int main() {
    char test1[!is_true<fu>::value];
    char test2[is_true<bar>::value];
    };

    and it failed there.

    Assuming I understand what you're trying to do, try this instead:

    template <class T>
    struct has_type
    {
    private:
    struct two {char x; char y;};
    template <class U> static two test(...);
    template <class U> static char test(typename U::type* = 0);
    public:
    static const bool value = sizeof(test<T>(0)) == 1;
    };

    struct fu { };
    struct bar { typedef int type; };

    int main() {
    char test1[!has_type<fu>::value];
    char test2[has_type<bar>::value];
    }

    That passes for me everywhere I tried it (including gcc 4.0).

    -Howard
    Howard Hinnant, Sep 26, 2005
    #4
  5. "Howard Hinnant" <> wrote in message
    news:...
    [snip]
    > That passes for me everywhere I tried it (including gcc 4.0).


    That is wonderful Howard, it works great on GCC 3.4.2, thank you very much!

    --
    Christopher Diggins
    http://www.cdiggins.com
    christopher diggins, Sep 26, 2005
    #5
    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. SFINAE

    , Apr 26, 2006, in forum: C++
    Replies:
    4
    Views:
    464
    n2xssvv g02gfr12930
    Apr 26, 2006
  5. Dilip

    confused with SFINAE

    Dilip, Jul 6, 2006, in forum: C++
    Replies:
    1
    Views:
    324
    Victor Bazarov
    Jul 6, 2006
Loading...

Share This Page