繁体   English   中英

C++ 条件模板构造函数

[英]C++ Conditional template constructor

我有一个类似于智能指针的HandleID class。 以下是重要的部分:

template<class T>
class HandleID
{
// Only if T is not const
friend class HandleID<const T>;

public:
    HandleID();
    HandleID(int id);
    HandleID(const HandleID<T>& other);
    HandleID& operator=(const HandleID<T>& other);

    // Only if T is const
    HandleID(const HandleID<const removed T>& other);
    HandleID& operator=(const HandleID<const removed T>& other);


private:
    T* cachedPointer;
    int id;
};

现在我希望能够从 HandleID<T> 构造一个HandleID<const T> HandleID<T>而不是相反。 复制赋值运算符也是如此: HandleID<const T> = HandleID<T>应该是合法的,但不是HandleID<T> = HandleID<const T>

现在我考虑为此添加模板专业化或其他东西,但我确信有更好的方法来做到这一点。 请注意,非 const 版本必须将 const 版本添加为友元,才能访问构造函数/assignmnent 运算符中的私有成员。

SFINAE 方式...

为简化起见,您可以为无常量T添加using类型

using no_const_T = std::remove_const_t<T>;

T不是const时等于T ,当Tconst时不同。

然后,您可以仅在Tno_const_T不同时启用 SFINAE 构造函数/运算符

template <typename U = T,
          std::enable_if_t<not std::is_same_v<U, no_const_T>, int> = 0>
HandleID(const HandleID<no_const_T> & other);

template <int..., typename U = T, 
          std::enable_if_t<not std::is_same_v<U, no_const_T>, int> = 0>
HandleID& operator=(const HandleID<no_const_T>& other);

请注意,您必须检查T是否与no_const_T相等或不同,而不是直接使用T而是使用用T初始化的本地(方法)模板参数( U )。

- 编辑 -

OP问

当我想将声明(您提供的那个)和实现(例如在文件中进一步向下的 class 之外)分开时,语法是什么

这是一种谵妄。

以下是在 class 主体之外实现的启用 SFINAE 的方法的完整编译(愚蠢)示例。

#include <type_traits>

template <typename T>
class HandleID
 {
   friend class HandleID<T const>;

   using no_const_T = std::remove_const_t<T>;

   public:
      HandleID () {}
      HandleID (int) {}
      HandleID (HandleID<T> const &) {}

      template <typename U = T,
                std::enable_if_t<not std::is_same_v<U, no_const_T>, int> = 0>
      HandleID (HandleID<no_const_T> const &);

      HandleID & operator= (HandleID<T> &)
       { return *this; }

      template <int..., typename U = T,
                std::enable_if_t<not std::is_same_v<U, no_const_T>, int> = 0>
      HandleID & operator= (HandleID<no_const_T> const &);
 };

template <typename T>
template <typename U,
          std::enable_if_t<not std::is_same_v<U,
             std::remove_const_t<T>>, int>>
HandleID<T>::HandleID (HandleID<std::remove_const_t<T>> const &)
 { }

template <typename T>
template <int..., typename U,
          std::enable_if_t<not std::is_same_v<U,
             std::remove_const_t<T>>, int>>
HandleID<T> & HandleID<T>::operator=
   (HandleID<std::remove_const_t<T>> const &)
 { return *this; }

int main()
 {
   HandleID<int>       id0;
   HandleID<int const> idc0;

   HandleID<int>  id1{id0};  // copy constructor: compile
   //HandleID<int>  id2{idc0}; // constructor disabled: compilatrion error

   HandleID<int const>  idc1{idc0};  // copy constructor: compile
   HandleID<int const>  idc2{id0};   // constructor enabled: compile
 }

在 C++20 中,它将是:

template<class T>
class HandleID
{
friend class HandleID<const T>;

public:
    HandleID();
    HandleID(int id);
    HandleID(const HandleID<T>& other);
    HandleID& operator=(const HandleID<T>& other);

    HandleID(const HandleID<std::remove_const_t<T>>& other) requires (std::is_const_v<T>);
    HandleID& operator=(const HandleID<std::remove_const_t<T>>& other) requires (std::is_const_v<T>);

private:
    T* cachedPointer;
    int id;
};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM