array decay to pointer and overload resolution

前端 未结 3 899
余生分开走
余生分开走 2020-12-03 15:12

I want to be able to differentiate array from pointers in overload resolution :

class string {
public:
        string(const char* c_str);

        template&         


        
3条回答
  •  温柔的废话
    2020-12-03 16:10

    You can use SFINAE. This might not be the best way, but it should work ok:

    //thanks to dyp for further reduction
    template::value>::type>
    string(const T * const &) {std::cout << "const char *\n";}
    
    template //credit to jrok for noticing the unnecessary SFINAE
    string(const char(&)[N]) {std::cout << "const char(&)[" << N << "]\n";}
    

    Here's a live example.

提交回复
热议问题