How to avoid decay with template parameter deduction

前端 未结 2 2039
萌比男神i
萌比男神i 2021-01-04 05:28

Simplified:

// CHAR_TYPE == char, wchar_t, ...
template 
void Foo(CHAR_TYPE const (&value)[CHAR_COUNT]) no         


        
2条回答
  •  时光取名叫无心
    2021-01-04 06:19

    One idea that works is to remove the pointer and simply have T instead, with a std::enable_if_t::value> guard. Simplified example below:

    #include 
    #include 
    
    template
    void f(T const (&) [N])
    {
        std::cout << __PRETTY_FUNCTION__ << std::endl;
    }
    
    template::value>* = nullptr >
    void f(T)
    {
        std::cout << __PRETTY_FUNCTION__ << std::endl;
    }
    
    int main()
    {
        const char* str = "test";
        char str2[]{"test2"};
    
        f(str);
        f(str2);
    }
    

    Live on Coliru

提交回复
热议问题