gpt4 book ai didi

c++ - 当类是另一个模板的模板参数时,可以使用类模板参数推导吗?

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:50:18 26 4
gpt4 key购买 nike

我试过这样写代码:

#include <string>
#include <vector>
#include <utility>

using namespace std::string_literals;
int main(){
std::vector v{1,2,3};
std::pair p{1,"a"s};
//std::vector<std::pair> vp{{1,"a"s}, {2, "b"s},{3,"c"s}}; // ERROR
}

但是它在 pair 是 vector 的模板参数的行中给出了编译错误。

有没有办法让类模板推导在这里工作,或者我是否需要为对指定模板参数?

最佳答案

目前不支持。来自 [dcl.type.class.deduct]p2 :

A placeholder for a deduced class type can also [in variable declarations too as type] be used in the type-specifier-seq in the new-type-id or type-id of a new-expression, or as the simple-type-specifier in an explicit type conversion (functional notation). A placeholder for a deduced class type shall not appear in any other context.

正如您从我突出显示的句子中看到的那样,由于您的使用与上述任何地方都不匹配,因此您的代码格式错误。

你可以这样做:

std::vector vp{std::pair{1, "a"s}, {2, "b"s}, {3, "c"s}};

关于c++ - 当类是另一个模板的模板参数时,可以使用类模板参数推导吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50354358/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com