veselink1 / refl-cpp

Static reflection for C++17 (compile-time enumeration, attributes, proxies, overloads, template functions, metaprogramming).
https://veselink1.github.io/refl-cpp/md__introduction.html
MIT License
1.06k stars 77 forks source link

get_bases has 4 error #19

Closed runner0353 closed 4 years ago

runner0353 commented 4 years ago

clang++ myinheritance.cpp -o myinheritance -std=c++17 In file included from myinheritance.cpp:2: ./../refl.hpp:2231:37: error: static_assert expression is not an integral constant expression static_assert(has_bases(t), "Target type does not have a bases<A, B, ...> attribute.");


myinheritance.cpp:30:28: note: in instantiation of function template specialization 'refl::descriptor::get_bases<refl::descriptor::type_descriptor<Animal> >' requested here
    constexpr auto bases = get_bases(type);
                           ^
myinheritance.cpp:51:5: note: in instantiation of function template specialization 'print_bases<Animal>' requested here
    print_bases<Animal>();
    ^
In file included from myinheritance.cpp:2:
./../refl.hpp:2233:68: error: constexpr variable 'bases' must be initialized by a constant expression
            constexpr auto bases = get_attribute<attr::base_types>(t);
                                   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~
./../refl.hpp:2231:37: error: static_assert expression is not an integral constant expression
            static_assert(has_bases(t), "Target type does not have a bases<A, B, ...> attribute.");
                          ~~~~~~~~~~^~
myinheritance.cpp:30:28: note: in instantiation of function template specialization 'refl::descriptor::get_bases<refl::descriptor::type_descriptor<Dog> >' requested here
    constexpr auto bases = get_bases(type);
                           ^
myinheritance.cpp:52:5: note: in instantiation of function template specialization 'print_bases<Dog>' requested here
    print_bases<Dog>();
    ^
In file included from myinheritance.cpp:2:
./../refl.hpp:2233:68: error: constexpr variable 'bases' must be initialized by a constant expression
            constexpr auto bases = get_attribute<attr::base_types>(t);
                                   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~
4 errors generated.clang cannot be compiled, but g ++ can be compiled
veselink1 commented 4 years ago

This "issue" where clang complains that an expression is not a constant expression has come up before. Solving it usually involved some workarounds as I was unable to get to the core of the problem. Turns out that clang is (again) more standards-compilant and this behaviour is specified in expr.const#5. Thanks to Richard for his answer here.

constexpr functions in v0.9.1 which take a const T& descriptor now take a const T instead which fixes the issue and is standards-compliant.

veselink1 commented 4 years ago

I will also be working on better and more rigorous tests which should hopefully prevent this from happening in the future!