标题说明了所有这两种常用方法都不起作用.我错过了什么?
1.
class Cl {
static constexpr double PI;
};
constexpr double Cl::PI = 3.14;
Run Code Online (Sandbox Code Playgroud)
(26):错误C2737:'private:static double const Cl :: PI':'constexpr'对象必须初始化
2.
class Cl {
static constexpr double PI = 3.14;
};
Run Code Online (Sandbox Code Playgroud)
(26):错误C2864:'Cl :: PI':具有类内初始化程序的静态数据成员必须具有非易失性const整数类型
类型为'const double'
在两次尝试中,错误都在类中的同一行.我正在使用VisualStudio/MSVC 2013年11月CTP编译器.
请注意,使变量const不是解决方案,因为我想在constexpr函数和普通函数中使用此常量.
我有下面的代码,它基本上映射std::integer_sequence<>到std::array<>编译时:
#include <iostream>
#include <utility>
#include <array>
template<int...Is>
constexpr auto make_array(const std::integer_sequence<int, Is...>& param) // this works */
// constexpr auto make_array(std::integer_sequence<int, Is...> param) // doesn't compile
{
return std::array<int, sizeof...(Is)> {Is...};
}
int main()
{
constexpr std::integer_sequence<int, 1,2,3,4> iseq;
// If I pass by value, error: the value of 'iseq' is not usable in a constant expression
constexpr auto arr = make_array(iseq);
for(auto elem: arr)
std::cout << elem << " ";
}
Run Code Online (Sandbox Code Playgroud)
只要make_array通过 …
我想尝试将项目从gcc迁移到clang ++.我承认我的无知,我不知道为什么以下的代码
template <typename T>
constexpr T pi{std::acos(T(-1.0))};
Run Code Online (Sandbox Code Playgroud)
使用g ++静默编译但clang ++产生错误
trig.hpp:3:13: error: constexpr variable 'pi<float>' must be initialized by a constant expression
constexpr T pi{std::acos(T(-1.0))};
Run Code Online (Sandbox Code Playgroud)
而且我希望有人比我更了解它,可以启发我.
注意:尝试使用-std = C++ 14和C++ 1y.在clang版本3.6.2(标签/ RELEASE_362/final)下失败.适用于g ++(GCC)5.2.0.
我已经看到在Quora上将参考变量声明为C++中的常量.
static constexpr const int& r = 3;
Run Code Online (Sandbox Code Playgroud)
那么,为什么都constexpr和const在单个语句中使用?
这种陈述的目的是什么?
我想if constexpr完全理解.
我理解,如果if constexpr(expr)在模板中使用,并且expr依赖于模板参数,那么在实例化期间,只有一个then/ else分支将被实例化,另一个将被丢弃.
我有两个问题:
expr不依赖于模板参数,那么if constexpr(expr)将不会丢弃任何分支?如果是,标准在哪里这样说?我没有看到标准有什么例外,丢弃只在expr依赖时发生.if constexpr模板以外有用吗?如果是,这有什么用例?你能举出一些例子来了解它的用处吗?我想创建一个对象,std::array<T, N>但问题是我只能使用返回constexpr类型的函数,否则编译器会抱怨。这里的问题是我需要根据另一个数组的大小来计算该数组的长度,可能是这样的:
template <typename T>
struct DataLength
{
template <typename iter>
size_t maxPossibleLength(iter begin, iter end)
{
size_t m_size = 0;
while (begin != end) {
m_size = m_size << 8 | std::numeric_limits<T>::max(); /* 0xff for uchar*/
begin++;
}
return m_size;
}
}
Run Code Online (Sandbox Code Playgroud)
我如何转换这个函数的输出,以便我可以使用它而不是N?
我写了一个简单的例子来解决我在写他的程序时遇到的问题。
\n\n在程序执行期间,当从函数返回值时,我得到 input1 和 input2 的值,这些值永远不会改变。然后稍后,在程序过程中进行各种计算后,我得到一个结果,该结果也不再可变。
\n\n我正在尝试使用 switch-case 来比较它们,但出现一个错误“\xe2\x80\x98input1\xe2\x80\x99 的值在常量表达式中不可用”。
\n\n#include <iostream>\n\nusing namespace std;\n\nchar getChar()\n{\n char c;\n cin >> c;\n return c;\n}\n\nint main()\n{\n // it doesn\'t work\n const char input1 = getChar();\n const char input2 = getChar();\n\n // it it works\n //const char input1 = \'R\';\n //const char input2 = \'X\';\n\n char result = getChar();\n switch(result)\n {\n case input1:\n cout << "input1" << endl;\n break;\n case input2:\n cout << "input2" << endl;\n break;\n }\n return 0;\n}\nRun Code Online (Sandbox Code Playgroud)\n 目前,我们有两个主要的编译时评估选项:模板元编程(通常使用模板结构和/或变量)和constexpr操作1。
template<int l, int r> struct sum_ { enum { value = l + r }; }; // With struct.
template<int l, int r> const int sum = sum_<l, r>::value; // With struct & var.
template<int l, int r> const int sub = l - r; // With var.
constexpr int mul(int l, int r) { return l * r; } // With constexpr.
Run Code Online (Sandbox Code Playgroud)
其中,我们保证可以在编译时对所有这四个值进行评估。
template<int> struct CompileTimeEvaluable {};
CompileTimeEvaluable<sum_<2, 2>::value> template_struct; // Valid.
CompileTimeEvaluable<sum<2, 2>> …Run Code Online (Sandbox Code Playgroud) 在以下代码中,我尝试存储对另一个类的 const 引用:
struct A {
};
struct B {
constexpr B(A const & _a) : a(_a) {}
A const & a;
};
int main() {
constexpr A s1;
constexpr B s2{s1};
}
Run Code Online (Sandbox Code Playgroud)
然而,编译器(gcc 11.1)抱怨:
cctest.cpp: In function ‘int main()’:
cctest.cpp:12:22: error: ‘B{s1}’ is not a constant expression
12 | constexpr B s2{s1};
|
Run Code Online (Sandbox Code Playgroud)
我无法弄清楚为什么s1不被视为常量表达式。s1本身是代码中的 constexpr。我知道这可能与引用的生命周期有关,但我无法弄清楚逻辑。在这个例子的代码中,我不想存储 A 的副本,我真的只想要一个引用或(智能)指针。所以:
s1不是常量表达式?非常感谢!
我的意思是,有可能以某种方式做这样的事情吗?
class Color {
public:
static constexpr Color BLACK = {0, 0, 0};
constexpr Color(int r, int g, int b) : r_(r), g_(g), b_(b) {}
private:
int r_;
int g_;
int b_;
};
Run Code Online (Sandbox Code Playgroud)
编译器Color在定义BLACK常量时抱怨类不完整。