Is it a good idea to enable the final
C++11 keyword conditionally to the standard version in a header file? I'm thinking of something like:
#if __cplusplus >= 201103L
# define MY_FINAL final
#else
# define MY_FINAL
#endif
// ...
class Derived : public Base
{
public:
virtual int f() MY_FINAL;
};
I have two doubts here:
final
will be ABI-compatible (it seems reasonable to assume so to me, and a quick check with g++
confirms that), 1) final
should never affect the ABI, it only affects whether the translation from C++ source code should succeed or fail, it has no required effect on the generated code.
2) You can make the compiler complain even in C++98 mode if you are prepared to use some non-standard extensions. G++ 4.7 supports __final
in C++98 mode with the same meaning:
#if __cplusplus >= 201103L
# define MY_FINAL final
#elif (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)
# define MY_FINAL __final
#else
# define MY_FINAL
#endif
I think clang++ accepts final
in C++98 mode, but issues a warning.
#
2 is pretty given. I'm not so sure about #1
, though. Logically, the C++11 compiler is well within it's rights to inline calls to f()
on a Derived object, for example, and not issue a virtual call, whereas a C++03 compiler is compelled to issue a virtual call. I'm not sure if this will actually make any difference, but it might.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.