constexpr c string concatenation,在constexpr上下文中使用的参数 [英] constexpr c string concatenation, parameters used in a constexpr context

查看:165
本文介绍了constexpr c string concatenation,在constexpr上下文中使用的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在探索我可以采取constexpr char const * concatenation从这个答案多远:
constexpr以连接两个或多个char字符串

I was exploring how far I could take the constexpr char const* concatenation from this answer: constexpr to concatenate two or more char strings

我有以下用户代码,显示我正在尝试的去做。看来编译器不能看到函数参数(a和b)是作为constexpr传递的。

I have the following user code that shows exactly what I'm trying to do. It seems that the compiler can't see that the function parameters (a and b) are being passed in as constexpr.

任何人都可以看到一个方法来使两个表示不工作下面,实际工作?通过这样的功能组合字符数组将是非常方便的。

Can anyone see a way to make the two I indicate don't work below, actually work? It would be extremely convenient to be able to combine character arrays through functions like this.

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示例

推荐答案

code>需要 const char(&)[N] ,在这两种情况下, code>,因此您可能会将您的函数更改为:

concat need const char (&)[N], and in both of your case, type will be const char*, so you might change your function as:

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

演示

这篇关于constexpr c string concatenation,在constexpr上下文中使用的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆