小编Ker*_*taş的帖子

为什么我无法推断出类模板参数之一?

我这里有一些代码

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"; }
Run Code Online (Sandbox Code Playgroud)

我可以创建一个对象

foo<int, 3, void(*)()> someObject(printString);
Run Code Online (Sandbox Code Playgroud)

或者

foo<int, 3, decltype(printString)> someObject(printString);
Run Code Online (Sandbox Code Playgroud)

但是当我尝试这样做时:

foo<int, 3> someObject(printString);
Run Code Online (Sandbox Code Playgroud)

我在 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, …
Run Code Online (Sandbox Code Playgroud)

c++ templates template-argument-deduction c++17

5
推荐指数
1
解决办法
251
查看次数