C++值在常量表达式中不可用
C++ value is not usable in a constant expression
我用C++编写了以下代码。我必须承认我对这门语言相当陌生,但我正在尽力学习:)
#include <iostream>
#include <cassert>
enum class Unit { km, m, cm };
template<int v, Unit u>
struct Measure {
static const int value = v;
static const Unit unit = u;
};
template<typename M1, typename M2>
struct Measures_same {
static const bool value() {
return M1::unit == M2::unit;
}
};
template<typename M1, typename M2>
struct SmallestUnit {
static const Unit value() {
switch(M1::unit) {
case Unit::km:
return M2::unit;
case Unit::m:
switch (M2::unit) {
case Unit::km:
return M1::unit;
case Unit::m:
return M2::unit;
case Unit::cm:
return M2::unit;
}
case Unit::cm:
return M1::unit;
}
};
};
template<typename M1, typename M2>
struct Measure_difference {
static const int value(){
if(Measures_same<M1,M2>::value()){
return 0;
}
Unit smallestValue = SmallestUnit<M1, M2>::value();
Unit largestValue;
if(M1::unit == smallestValue){
largestValue = M2::unit;
}
else{
largestValue = M1::unit;
}
switch(smallestValue) {
case Unit::m:
switch (largestValue) {
case Unit::km:
return 1000;
}
case Unit::cm:
switch (largestValue) {
case Unit::km:
return 1000 * 1000;
case Unit::m:
return 1000;
}
}
}
};
template<typename M1, typename M2>
struct Measure_add {
static const M1 value(){
if(Measures_same<M1,M2>::value()){
return Measure<(M1::value + M2::value), M1::unit>();
}
Unit smallestValue = SmallestUnit<decltype(M1::unit), decltype(M2::unit)>::value();
int const difference = Measure_difference<M1,M2>::value();
if(M1::unit == smallestValue){
return Measure<(M1::value + (M2::value * difference)), M1::unit>();
}
else{
return Measure<(M2::value + (M1::value * difference)), M2::unit>();
}
}
};
void testMeasuresSame(){
assert((Measures_same<Measure<10,Unit::km>, Measure<10,Unit::km>>::value()) == true);
assert((Measures_same<Measure<10,Unit::km>, Measure<10,Unit::m>>::value()) == false);
assert((Measures_same<Measure<10,Unit::cm>, Measure<10,Unit::m>>::value()) == false);
}
void testSmallestUnit(){
assert((SmallestUnit<Measure<1, Unit::km>, Measure<2, Unit::km>>::value()) == Unit::km);
assert((SmallestUnit<Measure<10,Unit::km>, Measure<10,Unit::m>>::value()) == Unit::m);
assert((SmallestUnit<Measure<10,Unit::cm>, Measure<10,Unit::m>>::value()) == Unit::cm);
}
void testMeasureDifference(){
assert((Measure_difference<Measure<1, Unit::km>, Measure<2, Unit::km>>::value()) == 0);
assert((Measure_difference<Measure<10,Unit::km>, Measure<10,Unit::m>>::value()) == 1000);
assert((Measure_difference<Measure<10,Unit::cm>, Measure<10,Unit::m>>::value()) == 1000);
assert((Measure_difference<Measure<10,Unit::cm>, Measure<10,Unit::km>>::value()) == 1000 * 1000);
}
void testMeasureAdd(){
Measure_add<Measure<10,Unit::cm>, Measure<10,Unit::cm>>::value();
}
int main() {
testMeasuresSame();
testSmallestUnit();
testMeasureDifference();
testMeasureAdd();
return 0;
}
现在,在运行线路Measure_add<Measure<10,Unit::cm>, Measure<10,Unit::cm>>::value();
时,我收到以下错误:
Scanning dependencies of target unitconverter
[ 95%] Building CXX object H08-unit-converter/CMakeFiles/unitconverter.dir/src/unitconverter.cxx.o
/unitconverter.cxx: In instantiation of ‘static const M1 Measure_add<M1, M2>::value() [with M1 = Measure<10, (Unit)2>; M2 = Measure<10, (Unit)2>]’:
/unitconverter.cxx:112:62: required from here
/unitconverter.cxx:78:63: error: could not convert ‘Measure<20, (Unit)2>()’ from ‘Measure<20, (Unit)2>’ to ‘const Measure<10, (Unit)2>’
return Measure<(M1::value + M2::value), M1::unit>();
^
/unitconverter.cxx:84:78: error: the value of ‘difference’ is not usable in a constant expression
return Measure<(M1::value + (M2::value * difference)), M1::unit>();
^
/unitconverter.cxx:82:19: note: ‘difference’ was not initialized with a constant expression
int const difference = Measure_difference<M1,M2>::value();
^
/unitconverter.cxx:84:78: note: in template argument for type ‘int’
return Measure<(M1::value + (M2::value * difference)), M1::unit>();
^
/unitconverter.cxx:87:78: error: the value of ‘difference’ is not usable in a constant expression
return Measure<(M2::value + (M1::value * difference)), M2::unit>();
^
/unitconverter.cxx:82:19: note: ‘difference’ was not initialized with a constant expression
int const difference = Measure_difference<M1,M2>::value();
^
/unitconverter.cxx:87:78: note: in template argument for type ‘int’
return Measure<(M2::value + (M1::value * difference)), M2::unit>();
^
我想从Measure_add特征中返回一个带有结果的度量。我真的不明白为什么我不能像这样使用差异。此外,我不确定我是否正确返回了这样的度量特征。 任何帮助都非常感谢!
仔细查看错误消息:
/unitconverter.cxx:78:63:错误:无法将
‘Measure<20, (Unit)2>()’
从‘Measure<20, (Unit)2>’
转换为‘const Measure<10, (Unit)2>
return Measure<(M1::value + M2::value), M1::unit>();
问题出现在下面的行(和类似)
static const M1 value(){
if(Measures_same<M1,M2>::value()){
return Measure<(M1::value + M2::value), M1::unit>();
}
...
值和单位都是您案例中的模板类型。当您添加M1::value
和M2::value
时,您正在更改(其中一个)模板参数和类型可能与M1
不同,除非M2::value
0
。
相关文章:
- 不能在初始值设定项列表中将非常量表达式从类型 'int' 缩小到'unsigned long long'
- 当一个值是非常量但用常量表达式初始化时使用constexpr
- gcc和clang在表达式是否为常量求值的问题上存在分歧
- 使用自动推导的 lambda 参数作为常量表达式
- 生成提升::hana::set 的常量表达式问题
- 为什么不能用常量表达式声明数组?
- 不是 lambda 函数中的常量表达式
- 函数调用在常量表达式中必须具有常量值
- 错误:constexpr 变量'struct2Var'必须由常量表达式初始化
- 如何在常量计算表达式中获取编译时错误?
- 关于在需要常量表达式的上下文中使用的glvalue常量表达式的问题
- 生成 constexpr 字符串表,不能产生常量表达式
- 整体模板参数。错误:在常量表达式中使用'this'
- 如何在满足常量表达式的同时将整数传递给指针,传递给 std::array<double、integer>?
- 编译器错误:函数调用在常量表达式中必须有一个常量值
- 错误:'new'不能出现在常量表达式中
- 我可以写出小于 -0.5 两个 ulps 的常量表达式双精度吗?
- 编译器在传递 const 变量时返回错误:模板参数不是常量表达式
- 为什么我不能在非常量表达式上使用此模板阶乘函数?
- C++ 使用变量而不是常量表达式初始化数组