Using SFINAE to check for global operator<<?

后端 未结 2 1191
你的背包
你的背包 2020-11-29 11:37

I want to have several overloaded, global to_string() functions that take some type T and convert it to its string representation. For the general

2条回答
  •  渐次进展
    2020-11-29 12:10

    The initializer of value on line 48 is not in a context where SFINAE works. Try moving the expression to the function declaration.

    #include 
    
    struct sfinae_base {
      typedef char yes[1];
      typedef char no[2];
    };
    
    template
    struct has_insertion_operator : sfinae_base {
    
      // this may quietly fail:
      template static yes& test(
          size_t (*n)[ sizeof( std::cout << * static_cast(0) ) ] );
    
      // "..." provides fallback in case above fails
      template static no& test(...);
    
      static bool const value = sizeof( test( NULL ) ) == sizeof( yes );
    };
    

    However, I have to question the amount of sophistication that is going into this. I see non-orthogonal mechanisms that will grind against each other (to_string vs. operator<<) and I hear poor assumptions getting tossed around (for example that operator<< is global vs a member, although the code as implemented looks OK in that regard).

提交回复
热议问题