带表达式的 C++ 模板参数

2024-03-10

我在使用 C++ 时遇到了麻烦。我希望能够将表达式作为参数放入模板中。这是我的代码:

#include <vector>
using namespace std;

vector<  ((1>0) ? float : int) > abc() {
}

int main(void){
  return 0;
}

这给了我错误:

main.cpp:11:14: 错误:模板参数 1 无效
main.cpp:11:14: 错误:模板参数 2 无效
main.cpp:11:15: 错误:‘{’ 标记之前预期有不合格的 id

最后,我希望能够将 1 和 0 替换为任何内容,并将类型名 T 和 U 替换为 float 和 int。为什么它认为有两个参数?我该如何解决这个问题?

(Sorry if this is a duplicate I did have a good look for solutions)

Use std::conditional:

#include <type_traits> 
std::vector<std::conditional<(1 > 0), float, int>::type> abc() {}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

带表达式的 C++ 模板参数 的相关文章

随机推荐