为什么编译器调用默认构造函数?

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)

Naw*_*waz 9

这是因为没有区别

 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)

演示:http://ideone.com/IjFUe

它们起作用,因为所有三种语法都强制它们是表达式,而不是变量声明.

但是,如果你试试这个(如评论中的@fefe sugguested):

 Foo<double(double)>(&sin);  //declaration, expression
Run Code Online (Sandbox Code Playgroud)

它不会起作用,因为它声明了一个引用变量,并且因为它没有被初始化,所以你会得到编译错误.请参阅:http://ideone.com/HNt2Z

  • O_________O ... [每日wtf] (4认同)
  • @Mehrdad:将行转换为`void`可能会起作用,因为它强制整行成为一个表达式,因此创建一个临时行. (3认同)