mercredi 1 août 2018

constexpr qualifier added on a non constexpr function does not trigger any warning

Seems the compiler ignores the constexpr qualifier when it is added to a non-constexpr function. Why is that?

The following code compiles fine and runs.

#include <iostream>
#include <string>
using std::string; using std::cout; using std::endl;

constexpr bool is_shorter(const string &lft, const string &rht) // this is not a constexpr function
{
    return lft.size() < rht.size();
}

int main()
{
    bool restul =  is_shorter("Hello", "World!");
    return 0;
}

Aucun commentaire:

Enregistrer un commentaire