c++ - clang vs gcc in abstract class handling in compile time -
one of these issues of nlohmann/json opensource library drew attention.
i have minimal reproduction of case does not compile under few version of desktop gcc (4.8, 4.9, tried 5+) compiles mac clang , android ndk's gcc 4.9
#include <limits> struct base { virtual void foo() = 0; }; int main() { (void)numeric_limits<base>::is_signed; } gcc trying instantiate std::numeric_limits base class instead of derived:
/usr/include/c++/4.8/limits: in instantiation of 'struct std::numeric_limits<base>': main.cpp:11:94: required here /usr/include/c++/4.8/limits:309:7: error: cannot allocate object of abstract type 'base' min() _glibcxx_use_noexcept { return _tp(); } i'm not quite sure if known compiler bug (in case of failure) or feature/relaxed rule (in case of success)
i've tried work around std::is_abstract not help, looks 'short-circuit' evaluation not happening in enable_if , error stays same
my question not how fix gcc weather compiler bug or code bug
edit: added "more minimal" example without standard library dependency:
template <typename t> struct foo { static t bar(); static constexpr bool value = true; }; struct abstract { virtual ~abstract() = 0; }; int main() { (void) foo<abstract>::value; } compiles on clang 3.9.0, gcc 7 snapshot emits error invalid return type of foo<abstract>::bar.
edit2: i'm bit surprised initial question edited without consent, not aware allows :) though think brought bit of confusion , wrong answers, because text , code not connected anymore
no, not bug. bad test.
description
the difference between gcc , clang in case way process template class functions:
gcc: of them in once.clang: 1 has been used.
in our first example, function min() doesn't called, , therefore, clang doesn't has problem it. gcc, parses functions , find out min() invalid.
in second example, same happens: bar() doesn't called , therefore clang ok being ill-formed. again, gcc, have problem it, although hasn't been used anywhere in program.
a test
saying bad not enough, let's fix it: example fail both gcc , clang same error (invalid abstract return type ‘base’ or allocating object of abstract class type 'base').
#include <limits> struct base { virtual void foo() = 0; }; int main() { (void)std::numeric_limits<base>::min(); }
Comments
Post a Comment