【发布时间】:2021-01-22 08:07:02
【问题描述】:
我这里有一些代码
template<typename T, std::size_t size, typename funcType>
struct foo
{
public:
foo(const funcType& func) : m_func(func) {}
~foo() {}
void m_call() { m_func(); }
private:
const funcType& m_func;
T x[size];
};
void printString() { std::cout << "some string\n"; }
我可以创建一个对象
foo<int, 3, void(*)()> someObject(printString);
或
foo<int, 3, decltype(printString)> someObject(printString);
但是当我尝试这样做时:
foo<int, 3> someObject(printString);
我在 g++ 10.2 上收到此错误
error: wrong number of template arguments (2, should be 3)
foo<int, 3> someObject(printString);
^
note: provided for 'template<class T, long unsigned int size, class funcType> struct foo'
struct foo
为什么我不能这样做?编译器不知道printString是什么类型吗?
如果我将foo 更改为
template<typename funcType>
struct foo
{
public:
foo(const funcType& func) : m_func(func) {}
~foo() {}
void m_call() { m_func(); }
private:
const funcType& m_func;
};
我可以正常创建
foo someObject(printString);
我错过了什么吗?
【问题讨论】:
标签: c++ templates c++17 template-argument-deduction