c++ - std::enable_if 未编译(无效的模板参数)

标签 c++ enable-if

我正在编写一些代码,应该接受 std::unique_ptr 并在其内容上激活运算符>>,如果指针为空则填充指针。

我试图通过使用 SFINAE 来限制不需要的调用,但我收到了一堆关于模板参数错误的错误消息。

代码:

#include "serializable.h"

template <class Archive, typename T>
Archive &operator>>( Archive &in, std::unique_ptr<typename std::enable_if<typename std::is_base_of<Serializable, T>::value, T>::type> &ptr )
{
    if ( !ptr )
        ptr.swap( Serializable::construct_from_stream( in ) ); // Constructs a Serializable derivative.

    in >> *ptr;
    return in;
}

错误:

// 1
error: template argument 1 is invalid
 Archive &operator>>( Archive &in, std::unique_ptr<typename std::enable_if<typename std::is_base_of<Serializable, T>::value, T>::type> &ptr )
                                                                                                                                     ^
// 2
error: template argument 2 is invalid

// 3
error: expected '::' before '&' token
 Archive &operator>>( Archive &in, std::unique_ptr<typename std::enable_if<typename std::is_base_of<Serializable, T>::value, T>::type> &ptr )
                                                                                                                                       ^
// 4
error: expected identifier before '&' token

// 5
error: request for member 'swap' in 'ptr', which is of non-class type 'int'
         ptr.swap( Serializable::construct_from_stream( in ) );
             ^
// 6
error: type/value mismatch at argument 1 in template parameter list for 'template<bool <anonymous>, class _Tp> struct std::enable_if'
     std::unique_ptr<typename std::enable_if<typename std::is_base_of<Serializable, T>::value, Serializable>::type>
                                                                                                           ^
// 7
error: 'Serializable' has not been declared
     ptr.swap( Serializable::construct_from_stream( in ) );
               ^
  • 这里的代码有什么问题?
  • 有没有更简洁的写法? (除了明显的“使用 std::unique_ptr”等。)

“enable_if_t”不起作用。但我认为这只是我做错事情的延伸,让 enable_if 一开始就不起作用。

我可以看出有些地方很不对劲,因为我还收到一条关于运算符 * 应用于 int 的错误消息...根本不应该滑入其中(如果 SFINAE 已正确实现)!

这里另一个有趣的问题是编译器无法识别 Serializable,即使它包含在它的正上方......如果答案不是微不足道的,我会单独查找。

我在 QtCreator 3.4.2/Qt 5.5.0 上使用 MinGW 4.9.2 x32 进行编译。

谢谢。

编辑:请不要建议只创建这样的函数:

Archive &operator>>( Archive &in, std::unique_ptr<Serializable> &ptr)...

我必须知道发送到此函数的对象的实际类型,不能依赖多态性。

最佳答案

删除 typename之前std::is_base_of<Serializable, T>::value (因为 std:is_base_of<...>::value 不是类型)并移动 enable_if部分超出参数类型(否则 T 将无法推导)。

template <class Archive, typename T>
typename std::enable_if<
    std::is_base_of<Serializable, T>::value,
    Archive &
>::type
  operator>>( Archive &in, std::unique_ptr<T> &ptr )

关于c++ - std::enable_if 未编译(无效的模板参数),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35000883/

相关文章:

c++ - 如何填充unique_ptr数组?

c++ - 使用 boostspirit 将 int 对解析为 vector

templates - 根据 iterator::value_type 选择函数实现

c++ - 为 X 的任何子类专门化类模板,而不向模板添加额外的类型参数

c++ - 我是否需要在测量性能时防止抢占

c++ - 返回虚函数的静态成员,缺少抽象类的 vtable

c++ - 检查函数是否可调用

c++ - 如何在 C++ 中通过 'int' 类型 'vector<int>' 来决定?

c++ - 无法使用 boost enable_if 匹配模板方法

c++ - 当在函数调用中使用 new 时,你释放了什么?如下所示在 boost 调用中