【发布时间】:2019-01-30 22:19:52
【问题描述】:
我已经实现了一个constexpr compile-time hash-function,如果调用它,它可以正常工作(即在编译时评估)
constexpr auto hash = CompileTimeHash( "aha" );
但我需要在实际代码中使用它作为函数的参数,如
foo( CompileTimeHash( "aha" ) ); // foo is NOT constexpr
由于特定原因,我不能使用长版本
constexpr auto hash = CompileTimeHash( "aha" );
foo( hash );
编译器 (VC++) 不会在短(第一种)情况下编译时散列。 有什么方法可以实现吗?
编辑:现在可以在此处找到涵盖 3 种情况的示例: https://godbolt.org/z/JGAyuE 只有 gcc 在所有 3 种情况下都能完成
【问题讨论】:
-
您如何知道哈希函数是在运行时计算的?
-
@Someprogrammerdude "你怎么知道哈希函数是在运行时计算的?"您可以通过在 static_assert 中调用它来检查
-
您使用的是哪个版本的 VC++?另外,我们可以得到一个minimal reproducible example 来测试其他编译器吗?
-
@Someprogrammerdude 你look at the disassembly
-
等等,调试或发布版本中的断点?调试不相关;发布是不可信的。再说一遍,你怎么知道? nm 是一个强有力的例子。
标签: c++ c++17 constexpr compile-time compile-time-constant