C使用一种方法无需警告即可实现不同类型分配的优雅方式?
发布时间:2020-12-16 05:04:08 所属栏目:百科 来源:网络整理
导读:我必须实现基本相同的功能,但不同的大小.具体来说就是…… type operator=(unsigned int);type operator=(unsigned long int);type operator=(unsigned long long int);type operator=(int);type operator=(long int);type operator=(long long int);type op
我必须实现基本相同的功能,但不同的大小.具体来说就是……
type& operator=(unsigned int); type& operator=(unsigned long int); type& operator=(unsigned long long int); type& operator=(int); type& operator=(long int); type& operator=(long long int); type& operator=(short int); //so on and so forth... 他们必须做同样的事情…(除了我应该考虑到不同的大小),主要的想法是“如果类型是最广泛使用任务的代码…否则执行转换和执行代码”.是否可以通过仅使用一种方法来避免所有这样的重复代码? (我只是不希望编译器在编译时给我一些警告……). 谢谢 解决方法
这适用于所有整数类型:
template< typename T,typename = std::enable_if_t<std::is_integral<T>::value> > type& operator= (T); 如果你想依赖于尺寸,可以通过sizeof(T)获得.您可以将其与您想要的最大尺寸进行比较. 如果你想要两个独立的函数,你需要将该子句放入其中并使用某种static-all. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |