- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
很多人都遇到过,我也遇到过。我在 C++ 中使用编译时字符串时遇到了困难。
我决定采用显然无法使用的方法:使用 template <char...>
类。
这是我想出来的,很普通,没什么特别的,而且也行不通。
template <char... chars> class string
{
public:
static constexpr const char value[] = {chars...};
constexpr string()
{
}
constexpr operator decltype(value) & () const
{
return value;
}
};
template <char... chars> constexpr const char string <chars...> :: value[];
我的想法是制作一个 string
实例 constexpr
可构造并公开某种类型的 constexpr 转换,以便它提供其内容。
现在,如果我这样做
static constexpr const char x[] = "ciao";
template <const char * str> void print()
{
std :: cout << str << std :: endl;
}
print <x> ();
这有效并说 ciao
.我得到 ciao
如果我这样做也是
std :: cout << string <'c', 'i', 'a', 'o'> {} << std :: endl;
或
print <string <'c', 'i', 'a', 'o', '\0'> :: value> ();
但是当我这样做的时候
print <string <'c', 'i', 'a', 'o', '\0'> {}> ();
我得到:No matching function for call to print
.
我肯定遗漏了什么。做我想做的事是不可行的吗?让实例执行 constexpr 转换以某种方式返回 value
?如果可行,我将能够在编译时轻松地进行运算符和字符串操作,“唯一”的缺点是 super 无聊 'i', 'n', 'i', 't', 'i', 'a', 'l', 'i', 'z', 'a', 't', 'i', 'o', 'n'
.
我做了另一个完美的实验。
template <char... chars> class string
{
public:
constexpr string()
{
}
constexpr operator size_t () const
{
return sizeof...(chars);
}
};
template <size_t length> void print()
{
std :: cout << length << std :: endl;
}
print <string <'c', 'i', 'a', 'o'> {}> ();
它打印出一个漂亮的 4
.
最佳答案
在 C++14 中,模板实参对模板非类型参数的限制是:
A template-argument for a non-type, non-template template-parameter shall be one of:
* for a non-type template-parameter of integral or enumeration type, a converted constant expression (5.19) of the type of the template-parameter; or
* the name of a non-type template-parameter; or
* a constant expression (5.19) that designates the address of a complete object with static storage duration and external or internal linkage or a function with external or internal linkage, including function templates and function template-ids but excluding non-static class members, expressed (ignoring parentheses) as&
id-expression, where the id-expression is the name of an object or function, except that the&
may be omitted if the name refers to a function or array and shall be omitted if the corresponding template-parameter is a reference; or
* a constant expression that evaluates to a null pointer value (4.10); or
* a constant expression that evaluates to a null member pointer value (4.11); or
* a pointer to member expressed as described in 5.3.1; or
* a constant expression of typestd::nullptr_t
.
在您的示例中,string<'c', 'i', 'a', 'o', '\0'>{}
这些都不是。请注意,第一个项目符号仅限于整数或枚举类型,并且 const char*
两者都不是(整数类型的异常(exception)是允许您的“进一步实验”示例编译)。其他项目符号均不适用。因此,符合标准的 C++14 应该拒绝您的代码。
解决方法是直接传递底层字符数组:
print<string<'c', 'i', 'a', 'o', '\0'>::value>();
或者将类型本身作为模板类型参数,并打印::value
在函数内:
template <class T>
void print() {
std::cout << T::value << std::endl;
}
print<string<'c', 'i', 'a', 'o', '\0'>>();
或者...
你会很高兴知道在 C++17 中,事情变得更好了。参见 N4198 , 以及激励性的例子:
template<int *p> struct A {};
int n;
A<&n> a; // ok
constexpr int *p() { return &n; }
A<p()> b; // error
该论文提出了对模板非类型参数的较少限制,这将使上述示例合式。新的措辞如下:
A template-argument for a non-type template-parameter shall be a converted constant expression (5.20) of the type of the template-parameter. For a non-type template-parameter of reference type or pointer type, the value of the constant expression shall not refer to (or for a pointer type, shall not be the address of):
* a subobject (1.8),
* a temporary object (12.2),
* a string literal (2.14.5),
* the result of atypeid
expression (5.2.8), or * a predefined__func__
variable (8.4.1).
就是这样。其他一切都是允许的。
自 string<'c', 'i', 'a', 'o', '\0'>{}
转换为一个不是那些东西的指针,这个例子就变得合式了。 Clang 3.8 将在 c++1z 模式下(正确)编译您的示例,但不会在 c++14 模式下(正确)编译。 GCC 还没有实现这个。给他们时间。
关于c++ - Constexpr 转换为 const char[],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38173469/
在 ubuntu gcc 8.0 中: void bar(){} constexpr int foo(int a) { if (a <=0 ) bar(); retur
考虑一个在运行时只包装一个值的类: template class NonConstValue { public: NonConstValue(const Type& val)
在试验 constexpr 函数和模板(以及非类型模板参数)时,我偶然发现了一个现象,我无法理解是哪条规则使它生效。 所以根据 constexpr-s 的规则,我的问题本质上是“为什么会发生这种情况”
我正在阅读 Nicolai M. Josuttis 所著的“C++ 17 The Complete Guide”一书,无法理解以下示例 auto squared1 = [](auto val) con
(使用 g++ 7.0 主干。) 给定以下“类型到值包装”实用程序... template struct type_wrapper { using type = T; }; // "Wraps" a
我编写了一些代码,它能够根据调用站点提供与给定函数关联的字符串(通过函数指针和并行数组的tuple)来分派(dispatch)给函数。 dispatch 函数不直接接受字符串,而是接受 Callabl
如果我想使用一些方便的东西,比如 make_array 我没有机会先声明我的数组,然后再像“早些时候”那样进行定义,因为我的 var 类型不可用定义前。 所以我找到了这个答案: Undefined r
使用 gcc (HEAD 7.0.0 201612) 我惊讶地发现这有效: constexpr long value(const char *definition) { if (definit
我有这个片段。 #include #include struct JustStr { JustStr(const std::string& x) : val(x) {} stati
我找不到任何关于新 C++17 if 初始化语法的信息和“constexpr if”在: http://open-std.org/JTC1/SC22/WG21/docs/papers/2016/p01
考虑以下函数: template auto concatenate(std::array &data1, std::array &data2) { std::array result;
假设我有以下对象: #include class Foo { public: constexpr Foo() {}; constexpr std::string foo() cons
我正在尝试使用 https://github.com/gdelugre/literal_ipaddr它说它是一个 C++17 constexpr implementation of inet_addr
我想重新定义unique_ptr用一个特殊的析构函数。因此,我使用以下代码尝试模仿 unique_ptr 的一些构造函数.遗憾constexpr施 worker 员拒绝 build ,我不知道为什么。
我想用结构名称的哈希值初始化一个结构成员。 constexpr uint32_t myHash(const char* const data) { //Some code for hash r
我正在尝试编译 C++ 库(使用 gcc 5.3.1-14ubuntu2)并遇到此类错误: > In file included from > /root/pitchfork/workspace/un
设置: 我有一个使用 SIMD 内部函数的函数,我想在一些 constexpr 函数中使用它。 为此,我需要将其设为 constexpr。但是,SIMD 内在函数没有标记为 constexpr,编译器
这是一个简化的代码示例,旨在生成任意值序列(在 std::iota 的意义上)和在它们之上的不同类别的迭代器: struct delta { template void inc(I&
考虑以下函数: template auto concatenate(std::array &data1, std::array &data2) { std::array result;
我偶然发现了调用非 constexpr 函数的 constexpr 模板函数:在以下代码段中,由于调用了非 constexpr set,bar 无法按预期编译,但 foo 可以编译。谁能告诉我 foo
我是一名优秀的程序员,十分优秀!