模板化类中运算符 + 重载的值的恢复

Restinction on values for operator+ overload in templated class

本文关键字:重载 恢复 运算符      更新时间:2023-10-16

我正在用模板做一些练习,但我遇到了这个问题。我有一个类,它表示具有指定编译时的基础类型和起始值的计数器。我想支持计数器类型之间以及具有正确类型的整数类型之间的算术运算符。例如,我想拒绝编译时将uint8_t作为基础类型的计数器的总和,变量类型为int。目前为止,一切都好。

我得到的是,即使是应该正确的操作也无法编译。这里主要部分来显示问题。

#include <iostream>
#include <type_traits>
#include <limits>

template <typename Source, typename Dest, std::enable_if_t<std::is_integral<Source>::value && std::is_integral<Dest>::value, int> =0>
struct type_not_narrow{
static constexpr bool value= std::numeric_limits<Source>::max() <= std::numeric_limits<Dest>::max();
};
template<typename T, T N, std::enable_if_t<std::is_integral<T>::value, int> =0>
class basic_counter{
T cnt;
basic_counter(T initializer): cnt(initializer){}
public:
basic_counter(): cnt(N){};
template <typename T1, T1 N1, typename T2, T2 N2>
friend basic_counter<typename std::common_type<T1,T2>::type, N1+N2> operator+(const basic_counter<T1, N1>& lhs,
        const basic_counter<T2, N2>& rhs);
template <typename X, typename U, U M, std::enable_if_t<type_not_narrow<X, U>::value, int>>
friend basic_counter<U,M> operator+(const basic_counter<U, M> lhs, const X step);
operator T() const{
return cnt;
}
};
template <typename T1, T1 N1, typename T2, T2 N2>
basic_counter<typename std::common_type<T1,T2>::type, N1+N2> operator+(const basic_counter<T1, N1>& lhs,
 const basic_counter<T2, N2>& rhs){
return {lhs.cnt+rhs.cnt};
}
template<typename X, typename T, T N, std::enable_if_t<type_not_narrow<X, T>::value, int> =0>
basic_counter<T,N> operator+(const basic_counter<T, N> lhs, const X step){
basic_counter<T,N> c=lhs;
c.cnt+=step;
return c;
}

int main() {

basic_counter<unsigned long int, 1> c1;
basic_counter<unsigned long int, 2> c2;
basic_counter<unsigned long int, 3> c3=c2+c1;
unsigned long int ul=5;
int i=5;
char ch=5;
basic_counter<unsigned long int, 2> c4=c2+5;
basic_counter<unsigned long int, 2> c5=c2+i;
basic_counter<unsigned long int, 2> c5bis=c2+ch;
basic_counter<unsigned long int, 2> c6=c2+ul;

//should not compile due to narrowing (and it's correct)
//basic_counter<unsigned long int, 2> c7=c2+5.2;
//basic_counter<uint8_t,0> c8;
//c8=c8+i;
basic_counter<uint8_t,0> c9;
//this should compile
c9=c9+1;
//this should not
c9=c9+2000;
return 0;
}

对于最后一行,我希望它编译,如果整型操作数可以成功转换为不缩小的类型。例如,添加 1 应该编译,而添加 2000 不应该,因为 1 可以用uint8_t表示,而 2000 不能。

如果您更正注释中提到的未定义行为,则此表达式:

c9=c9+1;

编译失败,因为 1 是int文字。编译器为此抛出了一些奇怪的错误,因为它试图通过调用隐式强制转换运算符将c9转换为整数并调用私有值构造函数来使整个事情正常工作。

所以这条线会起作用:

basic_counter<uint8_t,0> c10=c9+uint8_t(1);

这是一个你必须有一个尴尬的折衷方案,因为仍然不可能将函数参数的值用于模板参数和静态断言(除非P1045R1被采用(。