因为没有区别
Foo<double(double)>(sin);
和
Foo<double(double)> sin;
两者都声明了一个名为 sin 的变量。
括号是多余的。您可以放置任意数量的括号。
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
都是一样的!
如果你想创建类的临时实例,将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
}
输出:
called
called
called
演示:http://ideone.com/IjFUe
它们有效,因为所有三种语法都强制它们成为表达式,而不是 variable 声明。
但是,如果你尝试这个(正如@fefe 在评论中建议的那样):
Foo<double(double)>(&sin); //declaration, expression
它不起作用,因为它声明了一个引用变量,并且由于它没有初始化,你会得到编译错误。见:http://ideone.com/HNt2Z