相关疑难解决方法(0)

静态constexpr odr-used or not?

为什么下面的工作原理gcc却没有clang,(现场观看):

constexpr int giveMeValue() { return 42; }

struct TryMe {
  static constexpr int arr[1] = {
      giveMeValue()
  };  
};

int main() {
    int val = TryMe::arr[0];
    return val;
}
Run Code Online (Sandbox Code Playgroud)

我得到一个未解决的外部符号与clang.

TryMe::arr[0]一个对象?如果是的话,它是否经常使用?

c++ one-definition-rule constexpr c++11

9
推荐指数
1
解决办法
1758
查看次数

将静态constexpr类成员分配给运行时变量

我知道有很多类似的问题,但不知何故有不同的问题.这是关于以下情况:

#include <iostream>
#include <array>

template<typename T> class MyClass
{
public:
    static constexpr std::array<T,4> ARRAY {{4, 3, 1, 5}};
};

int main()
{
    constexpr std::array<int, 4> my_array(MyClass<int>::ARRAY); // works fine -> can use the ARRAY to initialize constexpr std::array

    constexpr int VALUE = 5*MyClass<int>::ARRAY[0]; // works also fine

    int value;
    value = my_array[0]; // can assign from constexpr
    value = MyClass<int>::ARRAY[0]; // undefined reference to `MyClass<int>::ARRAY

    std::cout << VALUE << std::endl;
    std::cout << value << std::endl;

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

据我所知 …

c++ templates one-definition-rule constexpr c++11

8
推荐指数
1
解决办法
894
查看次数

标签 统计

c++ ×2

c++11 ×2

constexpr ×2

one-definition-rule ×2

templates ×1