Constructor conditionally marked explicit

前端 未结 2 1255
借酒劲吻你
借酒劲吻你 2020-12-08 19:30

Update: conditional explicit has made it into the C++20 draft. more on cppreference

The cppreference std::tuple constructor page has a bunch of C++1

2条回答
  •  醉梦人生
    2020-12-08 20:15

    One way that seems to work with most compilers is to add a dummy parameter to one of the functions, to make them slightly different.

    // constructor is explicit if T is integral
    
    struct S {
      template ::value>::type>
      S(T t) {}
    
      template ::value>::type,
                typename dummy = void>
      explicit S(T t) {}
    };
    
    int main()
    {
       S  s1(7);
    
       S  s2("Hello");    
    }
    

    Compiles with MSVC 2015.

提交回复
热议问题