2016-08-29 32 views
5

我正在探索多遠我可以採取constexpr字符常量*從這個答案串聯: constexpr to concatenate two or more char stringsconstexpr C字符串拼接,在constexpr上下文中使用參數

我有以下的用戶代碼,準確顯示我米試圖做。看起來,編譯器看不到函數參數(a和b)以constexpr的形式傳入。

任何人都可以看到一種方法,使兩個我表明不工作,實際工作?能夠通過像這樣的功能組合字符數組將是非常方便的。

template<typename A, typename B> 
constexpr auto 
test1(A a, B b) 
{ 
    return concat(a, b); 
} 

constexpr auto 
test2(char const* a, char const* b) 
{ 
    return concat(a, b); 
} 

int main() 
{ 
    { 
    // works 
    auto constexpr text = concat("hi", " ", "there!"); 
    std::cout << text.data(); 
    } 
    { 
    // doesn't work 
    auto constexpr text = test1("uh", " oh"); 
    std::cout << text.data(); 
    } 
    { 
    // doesn't work 
    auto constexpr text = test2("uh", " oh"); 
    std::cout << text.data(); 
    } 
} 

LIVE example

回答

4

concat需要const char (&)[N],並在這兩個你的情況下,類型爲const char*,所以你可能會改變你的功能:

template<typename A, typename B> 
constexpr auto 
test1(const A& a, const B& b) 
{ 
    return concat(a, b); 
} 

Demo