我需要std::condition,但有两个以上的选择
I need std::conditional but with more than two choices
问题是,我有堆栈类模板,我想根据从文件中获取的数字或字符来决定创建哪种类型的对象。 所以而不是
if(T=='I')
{
myStack<int> teststack;
}
else if(T=='D')
{
myStack<double> teststack;
}
我想做一些允许我在"if"范围之外使用堆栈的事情
最接近的东西是 std::conditional,但在我的情况下,这应该像这样工作:
template<int type, class first, class second, class third>
所以我可以像这样使用它
int i;
input>>i;
myStack<i> teststack;
根据我拥有的数字,它应该是第一、第二或第三种类型。 我知道这不是最好的问题,但我只是有点困惑
获取i
值(从流中(的方式意味着它的值只能在运行时知道。
这意味着std::conditional
根本不适合您,因为条件表达式必须在编译时知道。
switch
语句将得到您需要的东西,但大多数实现本质上C++简化为if
语句链的switch
。
无论您想出什么解决方案,您都会有if
语句。
有一个老生常谈的C++道理,即"首先正确实施,然后开始优化"。 因此,一连串if
语句甚至switch
语句的幼稚方法是完全可以接受的,甚至是最好的方法,直到你发现你需要更有效的方法。
但是,如果要消除将i
与每个有意义的值进行比较的可能性,则可以使用类似std::map<char,
some-callable-type
>
。在映射中查找i
的值,并调用关联的可调用对象。
尝试类似操作:
#include<iostream>
#include<string>
#include<map>
#include<functional>
template<class T> struct myStack{};
template<class T> int doStuff()
{
myStack<T> mystack;
return 0;
}
int main()
{
char i;
std::map<char,std::function<int()>> doIt{
{'i', &doStuff<int>},
{'d', &doStuff<double>,},
{'l', []()->int{return 1;}},
{'s', &doStuff<std::string>}
};
std::cin>>i;
return doIt[i]();
}
(https://godbolt.org/z/fzhJc2(
如果可能性很少,您甚至可以使用std::array
。
std::conditional
s 可以组合成一个开关:
using U = std::conditional_t<
T == 'I',
First, std::conditional_t<
T == 'D',
Second, Third>>;
相关文章:
- 高级选择排序 - 在一次迭代中搜索两个元素
- 两个函数模板候选项.将一个参数作为引用后,选择不太专业的模板
- 我需要std::condition,但有两个以上的选择
- 最佳无分支有条件选择两个SSE2填充双打
- 包含两个C++文件的制作文件:如何选择要编译的文件
- 如何在C 中结合两个选择语句
- 在c++中选择两个纯虚拟函数中的一个
- 如何从视频中选择两个帧?OPENCV C
- C 通过随机选择其元素从两个元组制成元组
- 在两个函数之间选择的函子
- 为什么 a = (a+b) - (b=a) 是交换两个整数的糟糕选择
- 在两个不同的命名函数之间进行选择的特征的替代方法是什么?
- 如何从字符串中一次选择两个字符?到达字符串的第 i 个位置
- 在 C++11 中使用 SFINAE 在具有相同签名的两个函数之间进行选择
- RAII 在两个构造函数之间进行选择的方式
- 随机选择两个值
- C++同时使用两个不兼容的库,有什么选择
- 从两个字符串中选择一个进行输出
- c++中从两个相关向量中进行随机选择的最快方法是什么?
- 使用两个字符串分隔符选择文本文件的一部分