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

fix returning char* or const_string<> at the same time #13

Closed ticelo closed 4 years ago

veselink1 commented 4 years ago

That wouldn't really fix anything, would it? The return type of get_display_name is specified as const char*. Also, const_string is explicitly convertible to const char*. Are you running into an issue with the code as-is, or is this just an attempt at making the definition clearer?

ticelo commented 4 years ago

It was some code that wouldn't compile without the fix, just like this:

#include <iostream>

#include "third_party/refl-cpp/refl.hpp"

struct Label {
  char const* text() const { return "(nothing here)"; }
};

REFL_TYPE(Label)
REFL_FUNC(text, property())
REFL_END

int main() {
  Label l;
  for_each(refl::reflect(l).members, [&](auto member) {
    std::cout << get_display_name(member) << std::endl;
  });
}
> clang++ -std=gnu++17 fix1_demo.cpp
In file included from fix1_demo.cpp:2:
./third_party/refl-cpp/refl.hpp:2143:38: error: incompatible operand types ('const char *const' and 'const refl::util::const_string<4>')
                return friendly_name ? *friendly_name : t.name;
                                     ^ ~~~~~~~~~~~~~~   ~~~~~~
fix1_demo.cpp:21:22: note: in instantiation of function template specialization 'refl::descriptor::get_display_name<refl::descriptor::function_descriptor<Label, 0> >' requested here
         std::cout <<get_display_name(member)<<std::endl;
                     ^
1 error generated.
> clang++ --version
clang version 9.0.1
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
> git -C third_party/refl-cpp status
HEAD detached at d8bc094
nothing to commit, working tree clean

Have a nice day!