本文介绍了MSVC编译器甚至不优化使用/ Ox / EHsc的noexcept调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是一个例子。
#include <cstdlib>
template <typename TInteger>
class range_iterated_helper final
{
public:
constexpr range_iterated_helper(TInteger from, TInteger to) noexcept
: m_from(from), m_to(to)
{
}
struct const_iterator final
{
const_iterator& operator ++ () noexcept
{
++index;
return *this;
}
const TInteger operator * () noexcept
{
return index;
}
bool operator == (const_iterator const& other) const noexcept
{
return index == other.index;
}
bool operator != (const_iterator const& other) const noexcept
{
return index != other.index;
}
TInteger index;
};
constexpr const_iterator begin() const noexcept
{
return { m_from };
}
constexpr const_iterator end() const noexcept
{
return { m_to };
}
constexpr const_iterator cbegin() const noexcept
{
return { m_from };
}
constexpr const_iterator cend() const noexcept
{
return { m_to };
}
private:
TInteger m_from;
TInteger m_to;
};
template <typename TInteger>
constexpr range_iterated_helper<TInteger> make_integer_range(TInteger from, TInteger to) noexcept
{
return { from, to };
}
template <typename TInteger>
constexpr range_iterated_helper<TInteger> make_integer_range_to(TInteger to) noexcept
{
return { 0, to };
}
constexpr range_iterated_helper<size_t> make_indices(size_t size) noexcept
{
return { 0, size };
}
size_t f1(size_t n)
{
size_t result = 0;
for (auto x : make_indices(n))
result *= x;
return result;
}
size_t f2(size_t n)
{
size_t result = 0;
for (size_t x = 0; x < n; ++x)
result *= x;
return result;
}
https://godbolt.org/g/HUqhpq
https://godbolt.org/g/HUqhpq
我们有一些帮助代码,允许基于范围的语法为[0; n)迭代。使用/ EHsc编译
f1 时,调用( noexcept )函数 begin 和 end 保持不变。
是否有解决此问题的方法?
We have some helper code that allows ranged-based for syntax for [0; n) iteration. Whenf1 is compiled with /EHsc the calls to (noexcept) functions begin and end are kept without any optimizations. Are there any workarounds to this problem?
推荐答案
const_iterator(TInteger index_)
: index(index_)
{
}
这篇关于MSVC编译器甚至不优化使用/ Ox / EHsc的noexcept调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!