Meh*_*dad 5 c++ constructor default-constructor
为什么我收到以下错误?(为什么编译器试图调用默认构造函数?)
#include <cmath>
template<typename F> struct Foo { Foo(F) { } };
int main()
{
Foo<double(double)>(sin); // no appropriate default constructor available
}
Run Code Online (Sandbox Code Playgroud)
这是因为没有区别
Foo<double(double)>(sin);
Run Code Online (Sandbox Code Playgroud)
和
Foo<double(double)> sin;
Run Code Online (Sandbox Code Playgroud)
两者都声明了一个名字变量sin.
parens是多余的.您可以根据需要添加任意数量的parens.
int x; //declares a variable of name x
int (x); //declares a variable of name x
int ((x)); //declares a variable of name x
int (((x))); //declares a variable of name x
int (((((x))))); //declares a variable of name x
Run Code Online (Sandbox Code Playgroud)
一切都一样!
如果要创建类的临时实例,将其sin作为参数传递给构造函数,请执行以下操作:
#include<iostream>
#include <cmath>
template<typename F>
struct Foo { Foo(F) { std::cout << "called" << std::endl; } };
int main()
{
(void)Foo<double(double)>(sin); //expression, not declaration
(Foo<double(double)>(sin)); //expression, not declaration
(Foo<double(double)>)(sin); //expression, not declaration
}
Run Code Online (Sandbox Code Playgroud)
输出:
called
called
called
Run Code Online (Sandbox Code Playgroud)
它们起作用,因为所有三种语法都强制它们是表达式,而不是变量声明.
但是,如果你试试这个(如评论中的@fefe sugguested):
Foo<double(double)>(&sin); //declaration, expression
Run Code Online (Sandbox Code Playgroud)
它不会起作用,因为它声明了一个引用变量,并且因为它没有被初始化,所以你会得到编译错误.请参阅:http://ideone.com/HNt2Z