为什么我不能推导出类模板参数之一?

问题描述

我这里有一些代码

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,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);

我错过了什么吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)