- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
编辑:我以“if/else”情况为例,该情况有时可以在编译时解决(例如,当涉及静态值时,请参见<type_traits>
)。使下面的答案适应其他类型的静态分支(例如,多个分支或多标准分支)应该很简单。注意,这里不是使用模板元编程的编译时分支。
在这样的典型代码中
#include <type_traits>
template <class T>
T numeric_procedure( const T& x )
{
if ( std::is_integral<T>::value )
{
// Integral types
}
else
{
// Floating point numeric types
}
}
#include <type_traits>
template <class T>
inline T numeric_procedure( const T& x )
{
return numeric_procedure_impl( x, std::is_integral<T>() );
}
// ------------------------------------------------------------------------
template <class T>
T numeric_procedure_impl( const T& x, std::true_type const )
{
// Integral types
}
template <class T>
T numeric_procedure_impl( const T& x, std::false_type const )
{
// Floating point numeric types
}
最佳答案
TL; DR
有几种方法可以根据模板参数获得不同的运行时行为。 在这里,性能不是您的主要关注点,但灵活性和可维护性应该是您关注的重点。 在所有情况下,各种精简包装和常量条件表达式都将在任何适用的发行版编译器上进行优化。下面是有关各种折衷的小结(由@AndyProwl的this answer启发)。
运行时
您的第一个解决方案是简单的运行时if
:
template<class T>
T numeric_procedure(const T& x)
{
if (std::is_integral<T>::value) {
// valid code for integral types
} else {
// valid code for non-integral types,
// must ALSO compile for integral types
}
}
if/else
语句的两个分支实际上都需要针对所有类型T
进行编译,即使已知其中的一个分支都不采用。如果T
根据其性质包含不同的成员类型,则在尝试访问它们时会出现编译器错误。 template<class T>
T numeric_procedure_impl(const T& x, std::false_type)
{
// valid code for non-integral types,
// CAN contain code that is invalid for integral types
}
template<class T>
T numeric_procedure_impl(const T& x, std::true_type)
{
// valid code for integral types
}
template<class T>
T numeric_procedure(const T& x)
{
return numeric_procedure_impl(x, std::is_integral<T>());
}
std::is_integral<T>()
和对单行辅助函数的调用都可以在任何合适的平台上进行优化。
template<class T, class = typename std::enable_if<!std::is_integral<T>::value>::type>
T numeric_procedure(const T& x)
{
// valid code for non-integral types,
// CAN contain code that is invalid for integral types
}
template<class T, class = typename std::enable_if<std::is_integral<T>::value>::type>
T numeric_procedure(const T& x)
{
// valid code for integral types
}
template<class T, bool>
struct numeric_functor;
template<class T>
struct numeric_functor<T, false>
{
T operator()(T const& x) const
{
// valid code for non-integral types,
// CAN contain code that is invalid for integral types
}
};
template<class T>
struct numeric_functor<T, true>
{
T operator()(T const& x) const
{
// valid code for integral types
}
};
template<class T>
T numeric_procedure(T const& x)
{
return numeric_functor<T, std::is_integral<T>::value>()(x);
}
static if
失败的早期建议的
reboot(在D编程语言中使用)
template<class T>
T numeric_procedure(const T& x)
{
if constexpr (std::is_integral<T>::value) {
// valid code for integral types
} else {
// valid code for non-integral types,
// CAN contain code that is invalid for integral types
}
}
if
一样,所有内容都放在一个地方,但是这里的主要优点是,编译器将在未知的情况下完全丢弃
else
分支。一个很大的优点是您可以将所有代码都保留在本地,而不必像标签分派(dispatch)或部分模板专门化那样使用少量的辅助函数。
z==7
为最佳猜测)的一部分。
template<Non_integral T>
T numeric_procedure(const T& x)
{
// valid code for non-integral types,
// CAN contain code that is invalid for integral types
}
template<Integral T>
T numeric_procedure(const T& x)
{
// valid code for integral types
}
class
括号内的
typename
或
template< >
关键字。可以将其视为标记分派(dispatch)和SFINAE技术的概括。一些编译器(gcc,Clang)对此功能提供实验性支持。 Lite形容词指的是失败的Concepts C++ 11建议。
关于c++ - 编译器对编译时分支有什么作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23970532/
今天有小伙伴给我留言问到,try{...}catch(){...}是什么意思?它用来干什么? 简单的说 他们是用来捕获异常的 下面我们通过一个例子来详细讲解下
我正在努力提高网站的可访问性,但我不知道如何在页脚中标记社交媒体链接列表。这些链接指向我在 facecook、twitter 等上的帐户。我不想用 role="navigation" 标记这些链接,因
说现在是 6 点,我有一个 Timer 并在 10 点安排了一个 TimerTask。之后,System DateTime 被其他服务(例如 ntp)调整为 9 点钟。我仍然希望我的 TimerTas
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我就废话不多说了,大家还是直接看代码吧~ ? 1
Maven系列1 1.什么是Maven? Maven是一个项目管理工具,它包含了一个对象模型。一组标准集合,一个依赖管理系统。和用来运行定义在生命周期阶段中插件目标和逻辑。 核心功能 Mav
我是一名优秀的程序员,十分优秀!