gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:10:12 25 4
gpt4 key购买 nike

我用 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 特征返回一个 Measure 和结果。我真的不明白为什么我不能使用这样的差异。此外,我不确定我是否正确地返回了这样的 Measure 特征。非常感谢任何帮助!

最佳答案

仔细看报错信息:

/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>();

问题出现在下面一行(和类似行)

static const M1 value(){
if(Measures_same<M1,M2>::value()){
return Measure<(M1::value + M2::value), M1::unit>();
}
...

在您的案例中,值和单位都是模板类型。当您添加 M1::valueM2::value ,您正在更改(其中之一)模板参数和类型可能与 M1 不同除非M2::value0 .

关于C++ 值在常量表达式中不可用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42949456/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com