相关疑难解决方法(0)

类模板特化中的 decltype

我尝试在模板类中使用 decltype,如下所示:

#include <functional>
template <typename T>
class A
{
    typedef decltype(std::bind(&A::f, std::declval<A>())) some_type;

    void f();
};
Run Code Online (Sandbox Code Playgroud)

效果很好,但现在我想添加一个明确的专业化:

template <>
class A<void>
{
    typedef decltype(std::bind(&A::f, std::declval<A>())) some_type;

    void f();
};
Run Code Online (Sandbox Code Playgroud)

这次g++报错:

test.cpp:14:33: error: incomplete type 'A<void>' used in nested name specifier
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?我正在使用海湾合作委员会4.5。

编辑:如果我将声明移到void f();typedef 之上,按照 Johannes 的建议,我会得到(略有)不同的错误:

test.cpp:15:62: error: invalid use of incomplete type 'class A<void>'
test.cpp:13:1: error: declaration of 'class A<void>'
test.cpp:15:62: error:   initializing argument 2 of 'std::_Bind<typename std::_Maybe_wrap_member_pointer<_Tp>::type(_ArgTypes ...)> std::bind(_Functor, _ArgTypes ...) [with _Functor = void …
Run Code Online (Sandbox Code Playgroud)

c++ decltype template-specialization c++11

3
推荐指数
1
解决办法
2761
查看次数

标签 统计

c++ ×1

c++11 ×1

decltype ×1

template-specialization ×1