问题描述
我一直遇到以下问题:
std::tuple<Ts...> some_tuple = std::tuple<Ts...>();
for (int i = 0; i < 2; i++) {
const int j = i;
std::get<j>(temp_row) = some_value;
}
不编译:(在xcode中)它说没有匹配的函数调用'get'。
Does not compile: (in xcode) it says " no matching function call for 'get' ".
但是,以下工作正常:
std::tuple<Ts...> some_tuple = std::tuple<Ts...>();
for (int i = 0; i < 2; i++) {
const int j = 1;
std::get<j>(temp_row) = some_value;
}
是否与将常量的值定义为
Does it have something to do with defining the value of a constant to be the value of a variable?
谢谢!
编辑:区别在于 const int j = i;
与 const int j = 1;
推荐答案
这里的问题是C ++具有两种不同的常量。有编译时间常数和运行时间常数。编译时常数是在编译时已知的常数,并且是可以在模板或数组大小中使用的唯一有效常数。运行时常数是一个不能更改的值,但该值直到运行时才知道。这些常量不能用作模板或数组大小的值。因此,在
The problem here is C++ has two different kinds of constants. There are compile time constants and there are run time constants. A compile time constant is a constant that is know at compile time and is the only valid constant that can be used in a template or as an array size. A run time constant is a value that cannot change but the value is something that is not known until run time. These constants cannot be used as a value for a template or an array size. So in
std::tuple<Ts...> some_tuple = std::tuple<Ts...>();
for (int i = 0; i < 2; i++) {
const int j = i;
std::get<j>(temp_row) = some_value;
}
i
是一个使得 j
为运行时常数的运行时值,您无法使用它实例化模板。但是在
i
is a run time value which makes j
a run time constant and you cannot instantiate a template with it. However in
std::tuple<Ts...> some_tuple = std::tuple<Ts...>();
for (int i = 0; i < 2; i++) {
const int j = 1;
std::get<j>(temp_row) = some_value;
}
const int j = 1;
是一个编译时间常数,可用于实例化模板,因为模板的值在编译时是已知的。
const int j = 1;
is a compile time constant and can be used to instantiate the template as its value is known at compile time.
这篇关于元组std :: get()不适用于变量定义的常量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!