【发布时间】:2018-01-02 01:38:12
【问题描述】:
我发现了一个类似的问题here,但它并没有具体回答我的问题。我有一个简单的类模板,它只需要一个参数。它不存储任何成员变量,并且除了简单的构造函数之外没有任何方法。根据传入的类型,我需要在构造函数中分支我的逻辑。对于我正在尝试做的事情,该类的简单版本外壳看起来像这样。该类将对Type t进行一些处理,并将结果通过引用存储到std::string中。
template<class Type>
struct Test {
Test( Type t, std::string& str ) {
static_assert( std::is_arithmetic<Type>::value, "Arithmetic type required." );
if ( std::is_arithmetic<Type>::value ) { // check if type is arithmetic
// some variables here
// Note: I do not want to static_assert here if type is integral.
// If assert fails the else will not be executed.
if ( std::is_integral<type>::value ) {
// some code for integral types
} else {
// some other code for arithmetic non integral types (floating point types)
}
str = // some code.
} else {
// possibly throw some exception
}
}
};
这是基于data type 解决分支决策的适当方法吗?还是有更理想的有效方式来做到这一点?
- 我可以有一个带有几个成员变量和重载 2 或 3 个函数的默认构造函数
- 我可以对课程进行部分专业化(不是首选)。
- 我可以完全消除“类结构”并将其作为函数模板执行,但是,我更愿意实例化这种类型的对象。
【问题讨论】:
-
将
if更改为 C++17 的if constexpr。 -
@O'Neil 我对 C++17 的支持有限
-
@O'Neil 好的,令我惊讶的是;当项目设置中的字段留空时,Visual Studio 2017 CE 被设置为默认 C++14。有一个标志可以将其设置为 C++17 或 C++latest。我不得不做一些研究来找出答案。所以我确实支持
if constexpr( ... ) { }。我确实知道 MS Visual Studio 仅限于C++17的某些功能,而不是clang or gcc。漂亮的小保安! :)
标签: c++11 templates c++14 overloading partial-specialization