所以我有这样的真丑代码:是否有这样的事情了模板化的case语句
template <typename T>
std::conditional_t<sizeof(T) == sizeof(char),
char,
conditional_t<sizeof(T) == sizeof(short),
short,
conditional_t<sizeof(T) == sizeof(long),
long,
enable_if_t<sizeof(T) == sizeof(long long),
long long>>>> foo(T bar){return reinterpret_cast<decltype(foo(bar))>(bar);}
我使用嵌套conditional_t
s到做各种各样的case语句。有没有什么能够更加优雅地完成这件事,还是我需要制作自己的模板化案例陈述?
注:其实我知道,这使用的reinterpret_cast
不好:Why Doesn't reinterpret_cast Force copy_n for Casts between Same-Sized Types?
这段代码试图实现什么? – Slava 2015-02-24 15:11:43
你可以为char,short,int,long long做一个模板... – amchacon 2015-02-24 15:11:54
为什么这感觉就像你的依赖类型编程语言之后? – 2015-02-24 15:12:20