小编Liz*_*izi的帖子

带模板的条件返回类型

我想在C++中使用与模板相关的条件返回类型.我的环境中提供了C++ 11,14和17预览版.

我不是编程新手,但我是C++的新手,并且对某些功能有点困惑.

我想要实现的是:

如果模板是int32_t我的返回类型会int64_t,int16_t将返回int32_tint8_t会返回一个int16_t.

实际上我正在使用通用模板:

template <class T, class T2>
static T2 Foo(T bar1, T bar2) { //do something }

int64_t x = Foo<uint32_t, uint64_t>(555555, 666666);
Run Code Online (Sandbox Code Playgroud)

我想通过只输入参数类型来使这更加实用.

int64_t x = Foo<uint32_t>(555555, 666666);
int32_t x = Foo<uint16_t>(12000, 13000;
int16_t x = Foo<uint8_t>(88, 99);
Run Code Online (Sandbox Code Playgroud)

我尝试用以下方法实现它std::conditional:

template<typename OtherType,
        typename T = typename std::conditional<(sizeof(Type) <=   
        sizeof(OtherType)),
        OtherType, Type>::type>
Run Code Online (Sandbox Code Playgroud)

我愿意使用重载和疯狂的想法.

c++ conditional templates

4
推荐指数
1
解决办法
778
查看次数

标签 统计

c++ ×1

conditional ×1

templates ×1