2017-01-02 145 views
-1

我正在尝试这样做。C++ 11。如何从没有const的元组中获取元素

int flag = 0; 
if(big.size() <= small.size()) 
    flag = 1; //use float 

tuple<long, float> tup (1234.5678, 12341234.1234); 
auto foo = get<flag>(tup); 

但我得到的错误:

error: the value of 'flag' is not usable in a constant expression 
    cout << get<flag>(tup); 

- 和 -

note: 'int flag' is not const 
int flag = 0; 
+0

我的问题是玛丽有只小羊羔到指环王 – djent

回答

0

由于flag值在编译时不知道,它不能被使用的模板参数。

你需要使用类似:

tuple<long, float> tup (1234.5678, 12341234.1234); 
if (flag) 
{ 
    auto foo = get<1>(tup); 
} 
else 
{ 
    auto foo = get<0>(tup); 
} 
+0

我想富到虽然 – djent

+0

@djent整个范围存在,你必须重新考虑你的代码中的逻辑,因为你正在尝试的是行不通的。 –

+0

太糟糕了。谢谢 – djent

相关问题