【发布时间】:2015-09-12 00:27:34
【问题描述】:
这是我编写的代码:
#include <iostream>
#include <algorithm>
template <class T, T(*func)(T ...)> class Foo {
public:
template <class ...Args> Foo(const Args &...args) {
func(static_cast<T>(args)...);
}
};
int main() {
Foo<int, &std::max>(0, 1);
return 0;
}
... 如果参数数量无效, Foo::Foo 会抛出错误。 但我得到这个错误:
main.cpp: In function 'int main()':
main.cpp:10:23: error: could not convert template argument '& std::max' to 'int (*)(int, ...)
Foo<int, &std::max>(0, 1);
^
有什么问题?
【问题讨论】:
-
我不确定我是否理解你想要做什么。编译器已经检查函数的参数数量是否有效。你能澄清一下吗?
-
我觉得我做的这个“故事”已经足够澄清了:groups.google.com/a/isocpp.org/forum/?fromgroups#!topic/…
-
std::max是一个函数模板,所以你不能只将它转换为指向函数的指针,你需要一个实际的实例化,比如std::max<int>。您的代码仍然无法编译,因为std::max<int>的签名不是int(int, ...) -
我还是不明白你在做什么。对不起。
-
@Praetorian 将 '&std::max' 更改为 '&std::max
' 时仍然出现此错误。