【发布时间】:2015-11-09 10:22:36
【问题描述】:
让我们看看下面的代码:
#include <iostream>
class Test
{
private:
int x;
public:
Test(int _x) :
x(_x)
{
std::cout << "Im being constructed" << std::endl;
}
int getx()
{
return this->x;
}
friend std::ostream& operator<<(std::ostream& os, Test& other)
{
os << other.getx();
return os;
}
};
auto func(auto x)
{
std::cout << x << std::endl;
return x.getx();
}
int main()
{
auto y = func(20);
return 0;
}
编译器如何决定 (20) 是 int 还是 Test 对象? Test 的构造函数不明确,那么标准对此有何规定?
【问题讨论】:
-
1.
auto作为参数是概念 ts 的一部分。 2.x推导出为int,而不是Test -
gcc accepts auto parameters even though it is part of concepts-lite 虽然如果你使用
-pedanticgcc 会警告这不是iso C++。 -
好的,谢谢,所以标准允许对 lambdas 使用 auto 但不允许对函数使用,对吗?
-
是的,它允许用于通用 lambdas