- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我正在开发一个 C++11 项目,仅使用 clang++-3.4
,并决定使用 g++-4.8.2
进行编译以防产生的错误有任何差异。原来g++拒绝了clang++接受的一些代码。我已将问题简化为下面给出的 MWE。
enum { a };
template <class T>
struct foo
{
static constexpr auto value = a;
};
int main()
{
static constexpr auto r = foo<int>::value;
}
foo.cpp:5:23: error: ‘
const<anonymous enum> foo<int>::value
’, declared using anonymous type, is used but never defined [-fpermissive]static const auto value = A;
我需要一些帮助来回答以下两个问题:
哪个编译器对标准的解释是正确的?我假设一个编译器在接受或拒绝代码方面是正确的,而另一个是错误的。
我该如何解决这个问题?我无法命名匿名枚举,因为它来自第三方库(在我的例子中,枚举是 Eigen::RowMajor
和 Eigen::ColMajor
)。
最佳答案
GCC 错误地拒绝了您的代码段,根据 C++11 标准 (N3337),它是合法的。带有证明和解释的引文位于本文末尾。
解决方法 (A) - 添加缺少的定义
template <class T>
struct foo {
static constexpr auto value = a;
typedef decltype(a) value_type;
};
template<class T>
constexpr typename foo<T>::value_type foo<T>::value;
解决方法 (B) - 使用枚举的底层类型作为占位符
#include <type_traits>
template <class T>
struct foo {
static const std::underlying_type<decltype(a)>::type value = a;
};
如上所述,该片段是合法的C++11,可以在以下引用的部分中阅读。
我们什么时候可以使用没有链接的类型?
[basic.link]p8
有详细的措辞来描述一个类型何时是“无链接”,并声明未命名的枚举算作这种类型。
[basic.link]p8
还明确指出了不能使用这种类型的三个上下文,但没有一个上下文适用于我们的使用,因此我们是安全的。
A type without linkage shall not be used as the type of a variable or function with external linkage unless
- the entity has C language linkage (7.5), or
- the entity is declared within an unnamed namespace (7.3.1), or
- the entity is not odr-used (3.2) or is defined in the same translation unit
auto
吗?是的,这可以通过以下引用来证明:
7.1.6.4p
auto
specifier[dcl.spec.auto]
A
auto
type-specifier can also be used in declaring a variable in the condition of a selection statement (6.4) or an iteration statement (6.5), in the type-specifier-seq in the new-type-id or type-id of a new-expression (5.3.4), in a for-range-declaration, and in declaring a static data member with a brace-or-equal-initializer that appears within the member-specification of a class definition (9.4.2).
关于c++ - `static constexpr auto` 使用未命名枚举初始化的数据成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24018932/
在 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
我是一名优秀的程序员,十分优秀!