微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

C模板参数用表达式

我有麻烦C.我想把一个表达式放在模板中作为参数.这是我的代码
#include <vector>
using namespace std;

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

int main(void){
  return 0;
}

这给我错误

main.cpp:11:14: error: template argument 1 is invalid
main.cpp:11:14: error: template argument 2 is invalid
main.cpp:11:15: error: expected unqualified-id before ‘{’ token

最后,我想要替换1和0为任何,而float和int为typename T和U.为什么它认为有两个参数?那我该如何解决呢?
(对不起,如果这是一个重复的,我确实有一个很好的解决方案)

解决方法

使用std :: conditional:
#include <type_traits> 
std::vector<std::conditional<(1 > 0),float,int>::type> abc() {}

原文地址:https://www.jb51.cc/c/112180.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐