2016-08-29 33 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