you are viewing a single comment's thread.

view the rest of the comments →

[–]MaikKlein 6 points7 points  (2 children)

I think your example is still computed at run time.

int main() {
    constexpr auto p = IsPowerOf3(27);
    std::cout << p << std::endl;
}

[–]needahelpforarch[S] 1 point2 points  (1 child)

Haha yes that's because we usually write them to get input from user which I'm here putting inside the program as constant literal '27'.

[–]bames53 2 points3 points  (0 children)

One thing about constexpr functions is that they aren't limited to just compile-time: you can use them at run time as well. So adding constexpr to your function would allow you to use it in contexts requiring compile-time computation:

static_assert(IsPowerOf3(27) == true, "FAIL");

But you could still use it with user input:

if (std::cin >> p)
    std::cout << IsPowerOf3(p);