6

I wrote a function calculating the gcd of two numbers which uses std::swap in the case where the second parameter is greater than the first.

Some time later, I realised that std::swap is not constexpr, but my function still compiled and ran successfully.
I tried with MinGW-w64 8.1.0 and Visual C++ 2017 and it worked for both.

My first thought was that's because constexpr functions are allowed to be executed at runtime, so I tried std::integral_constant<int,gcd(32,12)>, and it worked.

However, I cannot use any of my own non-constexpr function (which is what I expect).

Here is my test code :

#include <utility>

inline void foo() noexcept {
}

template<typename T>
constexpr T gcd(T a, T b) {
    // foo();            // only works with non-constexpr j
    if(a<b) {
        std::swap(a, b); // works for both constexpr i and non-constexpr j
    }
    if(b==0) {
        return a;
    } else {
        return gcd(b, a%b);
    }
}

int main()
{
    constexpr int i = std::integral_constant<int, gcd(32, 12)>::value;
    int j = gcd(32,12);
}

So, my question is : why can I use std::swap in my function ?

9
  • 2
    swap is declared as constexpr. Take a look at here.
    – NutCracker
    Mar 11, 2019 at 9:53
  • @NutCracker Since C++20, not C++17. I'm not surprised by gcc which muddies the standard boundaries, but VS is usually not lax. Except if you used latest instead of C++17. Mar 11, 2019 at 9:53
  • 1
    @NutCracker that's not how constexpr works. Compiler has no power to arbitrary make functions constexpr not declared as such. What we have here is an "extension-made-standard".
    – Dan M.
    Mar 11, 2019 at 10:13
  • 5
    the swap-branch is never executed. try gcd(12, 32) (arguments flipped) and you get the compile error. Mar 11, 2019 at 10:34

1 Answer 1

8

Here is a relevant quote from cppreference:

A constexpr function must satisfy the following requirements:

  • ...
  • there exists at least one set of argument values such that an invocation of the function could be an evaluated subexpression of a core constant expression

There is a path that does not go through std::swap(), where a a>=b. In fact, for gcd(32, 12) the execution never goes through std::swap().

EDIT: I had a look at the C++14 draft. Section 7.1.5 The constexpr specifier. Paragraph 5 says:

For a non-template, non-defaulted constexpr function [...], if no argument values exist such that an invocation of the function or constructor could be an evaluated subexpression of a core constant expression (5.20), or, for a constructor, a constant initializer for some object (3.6.2), the program is ill-formed;

and the example they give is:

constexpr int f(bool b)
{ return b ? throw 0 : 0; } // OK

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.