vendredi 30 septembre 2016

Why can't C++ automatically figure out when to use constexpr without making us write the tag? [duplicate]

This question already has an answer here:

Item 15 of Scott Meyer's Modern C++ book: "use constexpr whenever possible". He says you can mark a function constexpr and still call it with values that aren't known at compile-time -- in that case it'll behave like any other runtime function, so you get the benefits of compile-time computation if possible but can still use it with non-compile time values too. The compiler will figure it out. But if that's true, then why can't we tag every function with constexpr (or better yet, don't tag any of them and let the compiler figure it out all the time)? If the compiler is able to detect whether the values are known at compile-time and automatically do the right thing when I tag the method as constexpr, then why can't it automatically do that same check even when I don't write constexpr?

Aucun commentaire:

Enregistrer un commentaire