Question about "substitution failure is not an error" concept, SFINAE

Discussion in 'C++' started by Aries Sun, Nov 29, 2007.

  1. Aries Sun

    Aries Sun Guest

    The following code can be compiled without error:

    typedef char True; // sizeof(True) == 1
    typedef struct { char a[2]; } False; // sizeof(False) > 1
    //...
    template <typename T> True isPtr( T * );
    template <typename T> False isPtr(T);

    #define is_ptr( e ) (sizeof(isPtr(e))==sizeof(True))

    int main() {
    int *a;
    cout << is_ptr(a) << endl;
    }

    why the following can compile:
    int test();
    int main() {
    cout << sizeof(test);
    }
     
    Aries Sun, Nov 29, 2007
    #1
    1. Advertising

  2. Aries Sun

    Aries Sun Guest

    Re: Question about "substitution failure is not an error" concept,SFINAE

    On Nov 29, 2:38 am, Aries Sun <> wrote:
    > The following code can be compiled without error:
    >
    > typedef char True; // sizeof(True) == 1
    > typedef struct { char a[2]; } False; // sizeof(False) > 1
    > //...
    > template <typename T> True isPtr( T * );
    > template <typename T> False isPtr(T);
    >
    > #define is_ptr( e ) (sizeof(isPtr(e))==sizeof(True))
    >
    > int main() {
    > int *a;
    > cout << is_ptr(a) << endl;
    >
    > }
    >
    > why the following can compile:
    > int test();
    > int main() {
    > cout << sizeof(test);
    >
    > }


    I got, because is call sizeof(isPtr(e)), there is a "(" and ")"
     
    Aries Sun, Nov 29, 2007
    #2
    1. Advertising

  3. On Wed, 28 Nov 2007 23:38:19 -0800 (PST), Aries Sun wrote:
    > The following code can be compiled without error:
    >
    > typedef char True; // sizeof(True) == 1
    > typedef struct { char a[2]; } False; // sizeof(False) > 1
    > //...
    > template <typename T> True isPtr( T * );
    > template <typename T> False isPtr(T);
    >
    > #define is_ptr( e ) (sizeof(isPtr(e))==sizeof(True))


    Note that normally you would do that like this:

    #include <iostream>

    struct true_type { enum { result = true }; };
    struct false_type { enum { result = false }; };

    template<typename T>
    struct is_pointer: public false_type
    {
    };

    template<typename T>
    struct is_pointer<T*>: public true_type
    {
    };

    int main()
    {
    std::cout << "char* : " << is_pointer<char*>::result << std::endl;
    std::cout << "int : " << is_pointer<int>::result << std::endl;
    }

    Because true_type and false_type do not have a common anchestor, you
    can now also do something like this:

    template<typename T>
    void handle_something_do(const T& t, false_type); // for non-pointer cases
    template<typename T>
    void handle_something_do(const T& t, true_type); // for pointer cases

    void handle_something(const T& t)
    {
    handle_something_do(t, is_pointer<T>() );
    }

    Especially doing a sizeof on a function's return value is somewhat
    weird in my opinion. Does the function get called or not? If it
    does, what does the function do? How should you implement it? Why
    should it take room in the executable, when all that is done is
    done at compile time?

    --
    Joel Yliluoma - http://iki.fi/bisqwit/
     
    Joel Yliluoma, Nov 29, 2007
    #3
  4. Re: Question about "substitution failure is not an error" concept,SFINAE

    On Nov 29, 9:15 am, Joel Yliluoma <> wrote:
    >
    > Especially doing a sizeof on a function's return value is somewhat
    > weird in my opinion. Does the function get called or not?
    >

    It doesn't get called - Alexandrescu uses this trick in one of his
    books, and it seems fairly common in TMP.
    Your way is more elegant though.
     
    tragomaskhalos, Nov 29, 2007
    #4
  5. Re: Question about "substitution failure is not an error" concept, SFINAE

    On Thu, 29 Nov 2007 04:29:39 -0800 (PST), tragomaskhalos wrote:
    > On Nov 29, 9:15 am, Joel Yliluoma <> wrote:
    >>
    >> Especially doing a sizeof on a function's return value is somewhat
    >> weird in my opinion. Does the function get called or not?
    >>

    > It doesn't get called - Alexandrescu uses this trick in one of his
    > books, and it seems fairly common in TMP.
    > Your way is more elegant though.


    Thanks. My questions (which you only quoted one of) in the context
    which I posted them were rhetorical by nature, though. They were
    questions which may be posed when reading such code.

    --
    Joel Yliluoma - http://iki.fi/bisqwit/
     
    Joel Yliluoma, Nov 29, 2007
    #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:
    479
    Pete Becker
    Jul 4, 2004
  2. Replies:
    3
    Views:
    1,672
    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:
    382
    Howard Hinnant
    Sep 9, 2005
  4. christopher diggins

    SFINAE problem.

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

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

Share This Page