3 ms·
Easier (and bending the rules a bit): #include <iostream> constexpr int f(int line) { return line; } #define f() f(__LINE__) int mai
by Someone 12y ago
Easier (and bending the rules a bit):
#include <iostream>
constexpr int f(int line)
{
return line;
}
#define f() f(__LINE__)
int main(int argc, const char * argv[]) {
constexpr int x = f();
constexpr int y = f();
std::cout << "x = " << x << "; y = " << y;
return EXIT_SUCCESS;
}
Also, I don't think the language designers required
"This lack of dependency must inherently mean that it will yield the same value upon each invocation having the same set of arguments"
to be true. http://en.cppreference.com/w/cpp/language/constexpr http://en.cppreference.com/w/cpp/language/constexpr only states:
"The constexpr specifier declares that it is possible to evaluate the value of the function or variable at compile time."
The draft of the C++ standard on my system states that as follows:
"Certain contexts require expressions that satisfy additional requirements as detailed in this sub-clause. Such expressions are called constant expressions. [ Note: Those expressions can be evaluated during translation. — end note ]"
- refp 12y ago> "This lack of dependency must inherently mean that it will yield the same value upon each invocation having the same set of arguments" That quote was written in a "this is what most of us used to think" sort of way, it was never intended to be read literally - though maybe I should clearify the intent. I certainly agree with your post, and your little hack is very cute; +1! :-)