繁体   English   中英

模板 class 中构造函数的条件定义

[英]Conditional definition of constructor in template class

我想要编译以下内容:

#include <type_traits>

template<typename T>
class C {
public:
    C() {}
    C( const C &that ) {}
};

void foo( const C<const char> &c );

int main() {
    C<const int> i;

    foo( C<char>{} );
}

当然,它没有,因为foo期望C<const char> ,而我正在传递C<char> 所以我想添加一个从C<T>C<const T>的隐式转换。 第一次尝试,我们将以下构造函数添加到C

C( const C< std::remove_const_t<T> > &that ) {}

这不起作用,因为如果T本身不是 const,则此定义与复制构造函数相同,这意味着我们重新定义了相同的构造函数两次:

so.cpp: In instantiation of ‘class C<char>’:
so.cpp:17:18:   required from here
so.cpp:9:5: error: ‘C<T>::C(const C<typename std::remove_const<_Tp>::type>&) [with T = char; typename std::remove_const<_Tp>::type = char]’ cannot be overloaded with ‘C<T>::C(const C<T>&) [with T = char]’
     C( const C< std::remove_const_t<T> > &that ) {}
     ^
so.cpp:7:5: note: previous declaration ‘C<T>::C(const C<T>&) [with T = char]’
     C( const C &that ) {}

第二次尝试,尝试使用enable_if

template< std::enable_if_t< std::is_const_v<T>, int > = 0 >
C( const C< std::remove_const_t<T> > &that ) {}

这是行不通的。 由于模板不依赖于构造函数的 arguments 中所做的任何推导,因此评估得太早:

so.cpp: In instantiation of ‘class C<char>’:
so.cpp:18:18:   required from here
so.cpp:10:5: error: no type named ‘type’ in ‘struct std::enable_if<false, int>’
     C( const C< std::remove_const_t<T> > &that ) {}
     ^

让我们人为地让它依赖于arguments,那么:

template<
        typename V,
        std::enable_if_t<
                std::is_const_v<T> && std::is_same_v<T, V>,
                int
        > = 0
>
C( const C< std::remove_const_t<V> > &that ) {}

我不确定为什么会失败。 我收到以下错误:

so.cpp: In function ‘int main()’:
so.cpp:24:10: error: invalid initialization of reference of type ‘const C<const char>&’ from expression of type ‘C<char>’
     foo( C<char>{} );
          ^~~~~~~~~
so.cpp:19:6: note: in passing argument 1 of ‘void foo(const C<const char>&)’
 void foo( const C<const char> &c );
      ^~~

我怀疑推论发生得太深,编译器无法理解它需要进行什么替换。

我该如何解决这个问题?

Ps 是的,我知道在这种情况下我可以使用隐式复制构造函数。

You can use SFINAE on function but function should be template, and condition should depend of that template, remember that T from class is fixed for the member function:

template<typename T>
class C {
public:
    C() {}
    C( const C &that ) {}

    template <typename U,
              std::enable_if_t<std::is_same_v<U, std::remove_const_t<T>>, int> = 0>
    C( const C<U> &that ) {}
};

演示

在 C++20 中会有很好的语法:

template<typename T>
class C {
public:
    C() {}
    C( const C &that ) {}
    C( const C< std::remove_const_t<T> > &that ) requires(std::is_const<T>::value) {}
};

对于您的代码:

template< std::enable_if_t< std::is_const_v<T>, int > = 0 >
C( const C< std::remove_const_t<T> > &that ) {}

不是替换上下文,参数由 class 修复,您有硬错误而不是预期的 SFINAE。

为了

template<
        typename V,
        std::enable_if_t<
                std::is_const_v<T> && std::is_same_v<T, V>,
                int
        > = 0
>
C( const C< std::remove_const_t<V> > &that ) {}

您可能有 SFINAE,但V不可推导,因此您必须提供它,但不能在调用站点执行它,因为它是构造函数。

简单的解决方法是提供默认值:

 template<
        typename V = T,
        std::enable_if_t<
                std::is_const_v<T> && std::is_same_v<T, V>,
                int
        > = 0
>
C( const C< std::remove_const_t<V> > &that ) {}

演示

(由于 V 是必需的 T,您可以删除std::is_same_v<T, V>但将std::is_const_v<T>更改为std::is_const_v<V>以获得 SFINAE)

暂无
暂无

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

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