Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/151.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
visualc&x2B+;constexpr提示 VisualC++是忽略了CONTXPR函数限定符而臭名昭著的,除非绝对需要。请看以下函数: constexpr int multiply(int l, int r) noexcept { return l * r; } 根据标准,Visual C++完全不允许在编译时对rValue:/P>进行评估。 auto three_times_four = multiply(3, 4);_C++_Visual C++_C++17 - Fatal编程技术网

visualc&x2B+;constexpr提示 VisualC++是忽略了CONTXPR函数限定符而臭名昭著的,除非绝对需要。请看以下函数: constexpr int multiply(int l, int r) noexcept { return l * r; } 根据标准,Visual C++完全不允许在编译时对rValue:/P>进行评估。 auto three_times_four = multiply(3, 4);

visualc&x2B+;constexpr提示 VisualC++是忽略了CONTXPR函数限定符而臭名昭著的,除非绝对需要。请看以下函数: constexpr int multiply(int l, int r) noexcept { return l * r; } 根据标准,Visual C++完全不允许在编译时对rValue:/P>进行评估。 auto three_times_four = multiply(3, 4);,c++,visual-c++,c++17,C++,Visual C++,C++17,我一直使用的解决方法是这种丑陋的力量: constexpr auto constexpr_three_times_four = ; auto not_constexpr_three_times_four = constexpr_three_times_four; // use not_constexpr_three_times_four in non-constexpr contexts // alternatively: template<auto val> inline con

我一直使用的解决方法是这种丑陋的力量:

constexpr auto constexpr_three_times_four = ;
auto not_constexpr_three_times_four = constexpr_three_times_four;
// use not_constexpr_three_times_four in non-constexpr contexts

// alternatively:
template<auto val>
inline constexpr auto ensure_constexpr = val;
auto not_constexpr_three_times_four = ensure_constexpr<multiply(3, 4)>;
那么,在这种情况下,我可以做些什么来提示编译器并避免这些丑陋的修复呢?

没有任何机制可以“提示”编译器(任何编译器)在编译时调用
constepr
函数。这不是
constexpr
的目的。它不是用来加速代码执行的工具。它是一个允许您在编译时进行计算的工具

C++20允许指定函数
consteval
,这确保函数必须在常量表达式中执行。但即使是这一功能也不是为了性能;这样他们就可以添加语言的新特性(如反射值),这些特性只能在编译时存在,并且不能泄漏到运行时代码中

C++20的
continit
允许您声明非常量表达式变量,其初始值设定项必须是常量表达式。这是最接近C++的代码< CONTXPRPR <代码> -A-性能。

但是,如果编译器的更高优化级别没有在编译时调用这些函数,那么编译器就是这样选择实现该功能的。

没有机制“提示”编译器(任何编译器)“应该在编译时调用
constepr
函数”。这不是
constexpr
的目的。它不是用来加速代码执行的工具。它是一个允许您在编译时进行计算的工具

C++20允许指定函数
consteval
,这确保函数必须在常量表达式中执行。但即使是这一功能也不是为了性能;这样他们就可以添加语言的新特性(如反射值),这些特性只能在编译时存在,并且不能泄漏到运行时代码中

C++20的
continit
允许您声明非常量表达式变量,其初始值设定项必须是常量表达式。这是最接近C++的代码< CONTXPRPR <代码> -A-性能。


但是,如果编译器的更高优化级别没有在编译时调用这些函数,那么这就是编译器选择实现该功能的方式。

看到
continit
被添加到语言中,我假设有一些隐含的理解,即这种“优化”应该(或应该)存在(ed)。我想我只需要等到编译器完成了C++20的实现,现在就坚持使用
Survey_constexpr
。看到
constinit
被添加到了语言中,我假设有一些隐含的理解,即这种“优化”应该(或应该)存在(ed)。我想我只需要等到编译器完成了C++20的实现,现在就坚持使用
sure_constexpr
;此对话已结束。评论不用于扩展讨论;这段对话已经结束。
namespace l
{
    constexpr ::std::uint32_t operator""_crc32(const char * p, ::std::size_t const size) noexcept
    {
        return crc32(p);
    }
}
//...
using namespace l;
search("foo"_crc32);//You don't want to evaluate this at runtime? too bad.