相关疑难解决方法(0)

为什么自动初始值设定项的自动和模板类型推导会有所不同?

我理解,给定一个支撑的初始化程序,auto将推导出一种类型std::initializer_list,而模板类型推导将失败:

auto var = { 1, 2, 3 };   // type deduced as std::initializer_list<int>

template<class T> void f(T parameter);

f({ 1, 2, 3 });          // doesn't compile; type deduction fails
Run Code Online (Sandbox Code Playgroud)

我甚至知道在C++ 11标准中指定的位置:14.8.2.5/5 bullet 5:

[如果程序有,则这是一个非推导的上下文]一个函数参数,其关联参数是初始化列表(8.5.4),但参数没有std :: initializer_list或者可能是cv-qualified std :: initializer_list的引用类型.[ 例如:

模板void g(T);

克({1,2,3}); //错误:没有推断T的参数

- 结束例子 ]

我不知道或不理解的是为什么存在这种类型演绎行为的差异.C++ 14 CD中的规范与C++ 11中的规范相同,因此标准化委员会可能不会将C++ 11行为视为缺陷.

有人知道为什么auto推导出支撑初始值设定项的类型,但是不允许使用模板吗?虽然对"这可能是原因"形式的推测性解释很有意思,但我对那些知道为什么标准是按原样编写的人的解释特别感兴趣.

c++ templates c++11 list-initialization template-argument-deduction

18
推荐指数
2
解决办法
1016
查看次数

初始化列表的隐式转换和完美转发

我正在尝试使用初始值设定项列表进行完美的转发工作。为了举例,我想要一个可变参数函数来调用另一个函数,并且仍然享受后者的初始化列表的自动转换:

\n
#include <iostream>  \n#include <vector>\n\nvoid hello(std::string const& text, std::vector<int> const& test)\n{\n  std::cout << "hello " << text << " " << test.size() << std::endl;      \n}\n\ntemplate<class ... Args>\nvoid f(Args&& ... args)\n{\n  return hello(std::forward<Args>(args)...);\n}\n\nint main()\n{\n  hello("world", {1,2,3});  // WORKS\n  f("world", std::vector<int>({1,2,3})); // WORKS\n  f("world", {1,2,3});  // COMPILER ERROR\n}\n
Run Code Online (Sandbox Code Playgroud)\n

错误是

\n
example.cpp: In function \xe2\x80\x98int main()\xe2\x80\x99:\nexample.cpp:21:21: error: too many arguments to function \xe2\x80\x98void f(Args&& ...) [with Args = {}]\xe2\x80\x99\n   21 |   f("world", {1,2,3});\n      |                     ^\nexample.cpp:12:6: note: declared here\n   12 | void …
Run Code Online (Sandbox Code Playgroud)

c++ c++17

6
推荐指数
1
解决办法
348
查看次数