模板化类中运算符 + 重载的值的恢复
Restinction on values for operator+ overload in templated class
我正在用模板做一些练习,但我遇到了这个问题。我有一个类,它表示具有指定编译时的基础类型和起始值的计数器。我想支持计数器类型之间以及具有正确类型的整数类型之间的算术运算符。例如,我想拒绝编译时将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被采用(。
相关文章:
- 继承函数的重载解析
- 你能重载对象变量名本身返回的内容吗
- 从父命名空间重载类型
- 使用C++中的模板和运算符重载执行矩阵运算
- 为什么这个运算符<重载函数对 STL 算法不可见?
- 重载操作程序时出错>>用于类中的字符串 memebr
- 一个关于在C++中重载布尔运算符的问题
- 不同翻译单元中不可重载的非内联函数定义
- 为什么使用SFINAE而不是函数重载
- 为什么我不能在 C++ 中的特定函数重载中调用同一函数的任何其他重载?
- 将重载的成员函数传递给函数模板
- c++:可变模板和函数重载
- 重载元组索引运算符-C++
- 如何使用重载的相等(==)运算符向测试用例添加描述
- 重载==不适用于二进制树
- 为什么Mat类的两个对象可以在不重载运算符+的情况下添加
- 用C++将哈希表写入文件并从文件中恢复
- 重载运算符new[]的行为取决于析构函数
- 正在尝试重载二进制搜索树分配运算符
- 模板化类中运算符 + 重载的值的恢复