有没有办法将auto作为参数传递给另一个函数?
int function(auto data)
{
//DOES something
}
Run Code Online (Sandbox Code Playgroud) 这个答案有一个这样的代码片段:
template<class T, class F>
auto f(std::vector<T> v, F fun)
-> decltype( bool( fun(v[0] ) ), void() )
{
// ...
}
Run Code Online (Sandbox Code Playgroud)
它真正编译和工作(至少在Ideone上).
那么,在这种情况下如何推断出类型?
c ++ 11标准真的允许下一行吗?
decltype( bool( fun(v[0] ) ), void() )
Run Code Online (Sandbox Code Playgroud)
我快速浏览了一下,看起来并不合适.在这种情况下,意识错误吗?
c ++ 11标准中的所有示例都是这样的,它们在decltype中只有一种类型:
struct A {
char g();
template<class T> auto f(T t) -> decltype(t + g())
{ return t + g(); }
};
Run Code Online (Sandbox Code Playgroud)
另一个例子 :
void f3() {
float x, &r = x;
[=] {
decltype(x) y1;
decltype((x)) y2 = y1; …Run Code Online (Sandbox Code Playgroud) 我遇到了以下形式的奇怪代码:
// Given the following definitions:
class B;
B b;
// Line of interest:
auto a(b);
Run Code Online (Sandbox Code Playgroud)
我以为它一定是拼写错误,但是经过一些实验发现它是可行的,并且似乎总是调用type的copy构造函数b(即使您有其他也可以具有匹配构造函数的类,即使您另外删除类型为的复制构造函数b。
我不知道这样一条语句的技术名称,所以我不确定如何在cppreference或StackOverflow中搜索它。编译器通常如何解析这种类型的语句,它在哪里记录?
-
回复:重复标记。我看不出他们如何解决这个问题。我已经知道auto使用模板类型推导,在这种情况下并不能说明任何事情。
我尝试了以下
auto ns=ms.capture[0].init,nl=ms.capture[0].len;
Run Code Online (Sandbox Code Playgroud)
因为我懒得查找init和len字段的类型。编译器当然不喜欢它。
我的理解是:
ttt xx,yy;
Run Code Online (Sandbox Code Playgroud)
相当于
ttt xx;ttt yy;
Run Code Online (Sandbox Code Playgroud)
那么应该同样的工作auto吗?也就是说,不应该
auto xx, yy;
Run Code Online (Sandbox Code Playgroud)
变得:
auto xx, auto yy;
Run Code Online (Sandbox Code Playgroud)
或者有什么我不明白的细节?还是我的编译器有问题?