constexpr does not work/apply inside function call

廉价感情. 提交于 2019-12-23 03:25:04

问题


I have implemented a constexpr compile-time hash-function, which works fine (i.e. is evaluated at compile-time) if called as

constexpr auto hash = CompileTimeHash( "aha" );

but I need to use it in actual code as an argument to a function as in

foo( CompileTimeHash( "aha" ) ); // foo is NOT constexpr

For a specific reason, I cannot use the long version

constexpr auto hash = CompileTimeHash( "aha" );
foo( hash );

The compiler (VC++) will not compile-time hash in the short (first) case. Is there any way to achieve this?

EDIT: An example covering the 3 cases is now found here: https://godbolt.org/z/JGAyuE Only gcc gets it done in all 3 cases


回答1:


Well, the as-if-rule always allows evaluation at runtime. However insane (and insanely complex) doing so might be.

Best shot to force your compiler to do it at compile-time, pass it through a template-argument:

A bit of setup:

template <auto x>
using make_integral_constant = std::integral_constant<decltype(x), x>;

template <auto x>
inline constexpr auto want_static = make_integral_constant<x>::value;

And use it like:

foo( want_static<CompileTimeHash( "aha" )> );

It works even without optimization, because unless you use an interpreter, doing it at runtime instead is too complex to do for no good reason.


Assigning to a constexpr-variable should also work. But it is actually easier to not evaluate at compile-time, so without optimization that happens anyway.

foo( []{ constexpr auto r = CompileTimeHash( "aha" ); return r; }() );



回答2:


If I understand correctly, your CompileTimeHash() return a int.

So what about

foo( sizeof(char[CompileTimeHash( "aha" )]) );

?

If CompileTimeHash() return only positive numbers, obviously.

If CompileTimeHash() return non negative numbers (positive numbers or zero), you can solve the zero problem (not acceptable as size for a C-style array) adding (inside) and subtracting (outside) 1

I mean

foo( sizeof(char[CompileTimeHash( "aha" )+1])-1u );


来源:https://stackoverflow.com/questions/52004389/constexpr-does-not-work-apply-inside-function-call

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!