Marking a function as constexpr
also makes it an inline function §[dcl.constexpr]/1:
A function or static data member declared with the constexpr specifier is implicitly an inline function or variable (7.1.6).
inline
, in turn, means you need include the definition of that function in every translation unit in which it may be used. That basically means constexpr
functions have to be either:
- restricted to use in one translation unit, or
- defined in a header.
Most typical functions that you want to declare in a header and define in a source file (and anything else that uses them just includes the header, then links against that source's object file) constexpr
simply won't work.
In theory, I suppose you could just move everything into headers and have only one source file that just includes all the headers, but this would hurt compile times drastically, and for most serious projects would require immense amounts of memory to compile.
A constexpr
function is also restricted in some ways, so for some functions it may not be an option at all. The restrictions include:
- virtual functions can't be
constexpr
.
- its return type must be a 'literal type" (e.g., no objects with non-trival ctors or dtors).
- all of its parameters must be literal types.
- the function body can't contain a
try
block.
- it can't contain a variable definition of a non-literal type, or anything with static or thread storage duration.
I've skipped over a couple rather obscure things (e.g., it also can't contain a goto
or an asm
statement), but you get the idea--for quite a few things, it just won't work.
Bottom line: yes, there are quite a few situations where this would be a poor idea.