0

With the new C++20 standard coming out, there are a number of relaxations in what we can include in a function marked constexpr (e.g. try-catch blocks) and what functions can be marked constexpr (e.g. virtual functions). While I understand the usefulness of this, as I've found myself attempting to write try-catch blocks in constexpr functions pre-C++20; it struck me as odd that these inherently runtime procedures could be evaluated at compile-time. As I dug around searching for an explanation, everything seemed to point back to these restrictions being arbitrary.

This got me wondering if it is the same for templates. For example, member function templates can't be virtual as in the following example:

class MyClass
{
public:
    template<typename T>
    virtual void doSomething();  // Error: 'virtual' is not allowed in a function template declaration
};

So is this restriction just an arbitrary constraint or is there something different between the compile-time evaluations of a constexpr function and a function template?

Note: I found this question to be helpful in explaining why it would be difficult (impossible??) to allow this, so my question is less about if it's possible and more of why it's easy for compilers to implement with constexpr and not with templates.

crdrisko
  • 454
  • 2
  • 5
  • 14
  • 2
    See [this](http://open-std.org/JTC1/SC22/WG21/docs/papers/2018/p0992r0.pdf). It explains the difference very well. – cigien Aug 31 '20 at 19:35
  • 2
    Why do you think it would be hard with constexpr? You're just doing the same thing you're doing at runtime, just during compile time -- and since you know at compile time what the most derived object is (otherwise it's ill-formed), it's not that much different from any other function call. – Barry Aug 31 '20 at 19:47
  • @cigien thank you, that was a very helpful way to think about it – crdrisko Aug 31 '20 at 19:50
  • @Barry I don’t think it would be hard with constexpr, I asked why it’s easy with constexpr and not templates – crdrisko Aug 31 '20 at 19:51
  • @crdrisko But those are very different things - why would the one have to do with the other? – Barry Aug 31 '20 at 20:01
  • 1
    @Barry based on my understanding of compile time (before I read the article cigien linked to), I assumed the evaluations of constexpr functions and function templates were similar, hence my incorrect logic of thinking what’s easy with one is easy with the other – crdrisko Aug 31 '20 at 20:18
  • @crdrisko: I don't understand your question. Virtual constexpr functions and virtual template functions basically have nothing to do with one another. What would make virtual constexpr functions difficult to implement? – Nicol Bolas Aug 31 '20 at 21:11
  • @NicolBolas I think the wording is misleading, probably better is ‘why aren’t virtual template functions as easy to implement as virtual constexpr functions?’ But if you look at my comment to Barry the link sent by cigien cleared up my misconceptions concerning the two. Not sure if the question needs to be deleted or not – crdrisko Aug 31 '20 at 21:20

0 Answers0