Thread: SFINAE trouble

  1. #1
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654

    SFINAE trouble

    Okay, so I always fail at applying std::enable_if properly for some reason. So maybe someone here who is better at this can tell why this isn't working?

    Code:
    template<bool B>
    struct Test
    {
    	template<typename T = void>
    	std::enable_if_t<B> test1() { std::cout << "test1\n"; }
    
    	template<typename T = void>
    	std::enable_if_t<!B> test2() { std::cout << "test2\n"; }
    };
    
    int main()
    {
    	Test<true> a;
    	Test<false> b;
    }
    Code:
    Error	1	error C2039: 'type' : is not a member of 'std::enable_if<false,void>'
    Error	2	error C2061: syntax error : identifier 'type'
    Error	3	error C2988: unrecognizable template declaration/definition	
    Error	4	error C2059: syntax error : '<end Parse>'
    Error	5	error C2143: syntax error : missing ';' before '}'
    Error	6	error C2238: unexpected token(s) preceding ';'
    Error	7	error C2061: syntax error : identifier 'type'
    Error	8	error C2988: unrecognizable template declaration/definition
    Error	9	error C2059: syntax error : '<end Parse>'
    Error	10	error C2334: unexpected token(s) preceding '{'; skipping apparent function body
    Last edited by Elysia; 09-04-2014 at 03:51 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Oh! I recall now. The enable_if construct has to depend on a template parameter. So if we do:

    Code:
    template<bool B>
    struct Test
    {
        template<bool B2 = B>
        std::enable_if_t<B2> test1() { std::cout << "test1\n"; }
     
        template<bool B2 = B>
        std::enable_if_t<!B2> test2() { std::cout << "test2\n"; }
    };
     
    int main()
    {
        Test<true> a;
        Test<false> b;
    }
    Then it starts working. I should keep this for future reference.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    I think you want to do
    Code:
    template<bool B>
    struct Test
    {
        template<bool B2=B>
        typename std::enable_if_t<B2>::type test1() { std::cout << "test1\n"; }
      
        template<bool B2=B>
        typename std::enable_if_t<!B2>::type test2() { std::cout << "test2\n"; }
    };
    enable_if::type will only be defined if the first template argument is true. You can also pass in a second template parameter, the typename that ::type should be typedefed to, which defaults to void.
    Last edited by Shakti; 09-04-2014 at 12:00 PM.
    STL Util a small headers-only library with various utility functions. Mainly for fun but feedback is welcome.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    std::enable_if_t<B> == typename std::enable_if<B>::type
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    Ah missed the _t, serves me for reading code on an empty stomach...
    STL Util a small headers-only library with various utility functions. Mainly for fun but feedback is welcome.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. SFINAE fun (or failure...)
    By Elysia in forum C++ Programming
    Replies: 25
    Last Post: 05-15-2013, 03:17 PM
  2. Could I get into trouble?
    By Queatrix in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 05-31-2007, 07:52 PM
  3. trouble
    By firefly in forum C++ Programming
    Replies: 4
    Last Post: 06-28-2005, 04:21 PM
  4. I'm having a bit of trouble...
    By gcn_zelda in forum Windows Programming
    Replies: 2
    Last Post: 01-01-2004, 06:02 PM
  5. Please Help Me,i'm In Trouble
    By ektrem^gal in forum C++ Programming
    Replies: 5
    Last Post: 04-30-2003, 04:47 AM