C++值在常量表达式中不可用

C++ value is not usable in a constant expression

本文关键字:表达式 常量 C++      更新时间:2023-10-16

我用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::valueM2::value时,您正在更改(其中一个)模板参数和类型可能与M1不同,除非M2::value0