- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有以下代码:
#include<chrono>
#include<iostream>
using namespace std::chrono_literals;
#define MSG "hello"
#define DUR 1000ms
class mwe{
public:
static constexpr auto msg = MSG;
static constexpr auto dur_1 = DUR;
static constexpr std::chrono::milliseconds dur_2 = DUR;
static const std::chrono::milliseconds dur_3;
static constexpr decltype(DUR) dur_4 = DUR;
};
constexpr std::chrono::milliseconds mwe::dur_2;
const std::chrono::milliseconds mwe::dur_3 = DUR;
constexpr decltype(DUR) mwe::dur_4;
int main(void) {
std::cout << "str: " << mwe::msg << std::endl;
std::cout << "dur_1: " << mwe::dur_1.count() << std::endl;
std::cout << "dur_2: " << mwe::dur_2.count() << std::endl;
std::cout << "dur_3: " << mwe::dur_3.count() << std::endl;
std::cout << "dur_4: " << mwe::dur_4.count() << std::endl;
}
如果我编译它(g++ 4.9),通过
g++ -std=c++14 -O2 test.cpp
一切都像预期的那样工作,但是如果我通过编译它
g++ -std=c++14 -O0 test.cpp
我收到以下错误:
undefined reference to `mwe::dur_1'
我个人最喜欢这种方式,dur_1 被定义和声明最多,但如果未启用优化,它不适用于我的版本中的 g++。因为我知道的所有其他方式(dur_2、dur_3、dur_4)都有其缺点(值冗余,没有自动类型推导,例如,如果我将 1000ms 更改为 1s,aso。)
您知道吗,如果这是一个 gcc 错误,编译可以在生产模式下运行,但如果没有优化就无法运行?
是否有另一种可能的方法来实现此功能,而无需在类外定义 dur_x 的位置?
最佳答案
这不是您的编译器中的错误。
您odr-used dur1
但从未定义它。而且,constexpr
和内联初始化程序都没有将该声明作为定义。
[C++11, C++14: 9.4.2/3]:
If a non-volatile conststatic
data member is of integral or enumeration type, its declaration in the class definition can specify a brace-or-equal-initializer in which every initializer-clause that is an assignment-expression is a constant expression (5.19). Astatic
data member of literal type can be declared in the class definition with theconstexpr
specifier; if so, its declaration shall specify a brace-or-equal-initializer in which every initializer-clause that is an assignment-expression is a constant expression. [ Note: In both these cases, the member may appear in constant expressions. —end note ] The member shall still be defined in a namespace scope if it is odr-used (3.2) in the program and the namespace scope definition shall not contain an initializer.
一如既往,优化级别可能会影响编译器能够和/或愿意报告此类错误的程度。
您可以编写以下内容来定义您的成员:
#include<chrono>
using namespace std::chrono_literals;
#define DUR 1000ms
struct T
{
static constexpr auto dur_1 = DUR;
};
constexpr decltype(T::dur_1) T::dur_1;
关于c++ - 没有优化的 g++ 4.9 上静态 constexpr 的 undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30831367/
在 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
我是一名优秀的程序员,十分优秀!