Reputation: 109
Say I have the following object:
#include <string>
class Foo
{
public:
constexpr Foo() {};
constexpr std::string foo() const { return "foo"; }
};
This is obviously wrong, std::string
is not a literal, cannot be returned from a constexpr
, and the compiler complains to that effect.
However, if I make that object a template and instantiate it in my program as something arbitrary like Foo<1>
, and then call the function, The compiler doesn't even give me so much as a warning.
#include <string>
template <int N>
class Foo
{
public:
constexpr Foo() {};
constexpr std::string foo() const { return "foo"; }
};
What is special about templated objects in this situation? (GCC 4.9)
Upvotes: 1
Views: 90
Reputation: 109
As per the comments, this is ill-formed, and no diagnostic is required.
Since the spec does not require diagnostics to alert writers, compilers don't have to do anything if you write this code, and the compiler is free to ignore the fact that the user thinks that their code should be able to be evaluated at compile time and carry on its merry way.
Now, if you're like me you're probably thinking "wait... why? This seems like the perfect case for a warning at the very least since what I wrote won't result in exactly what I expect."
This, apparently, is what the developers of clang thought too, so even though this code could go through compilation cleanly according to the letter of the law, clang throws full-blown errors:
Upvotes: 1