Closed grandseiken closed 5 months ago
Hi, @grandseiken, thanks for opening the issue.
I can't reproduce the issue using Ubuntu clang version 17.0.6. When I try to compile your code snippet, it compiles just fine.
The clue is in this line, though:
external/_main~_repo_rules~reflect_cpp/include/rfl/internal/../internal/get_field_names.hpp(97,15): note: in instantiation of template class 'rfl::Literal<internal::StringLiteral<22>{{{103, 101, 116, 95, 102, 105, 101, 108, 100, 95, ...}}}, internal::StringLiteral<22>{{{103, 101, 116, 95, 102, 105, 101, 108, 100, 95, ...}}}>' requested here
The ASCII strings 103, 101, 116, 95, 102, 105, 101, 108, 100, 95, ... translate to getfield...
I am fairly certain that for some reason get_field_names_str_view
is not doing what it is supposed to be doing. The function can be found in here:
https://github.com/getml/reflect-cpp/blob/main/include/rfl/internal/get_field_names.hpp
The question is why...we have rigorously tested this on clang and MSVC and I cannot reproduce the error. What is your OS? Can you think of anything that might be different on your machine?
I am also a bit confused...you said it compiles with "the microsoft compiler", but not Visual Studio 17.8.5? Isn't Visual Studio 17.8.5 by Microsoft? Could you clarify that?
clang-cl
is a compiler driver which allows building with clang on Windows while using the microsoft standard library. https://clang.llvm.org/docs/UsersManual.html#clang-cl it's (afaik) the easiest way to build with clang on windows.
Have you tried building with clang on windows at all?
I tried fixing it myself, but it seems the method you're using to get field names might just not work under clang-cl. I'm not sure if I'm invoking the function in exactly the same way as the library does, but the following program only prints get_field_name_str_view
, i.e. it seems the template arguments are not included in the function_name()
of std::source_location
.
#include <iostream>
#include <source_location>
#include <string_view>
struct test {};
template <class T, auto ptr>
consteval auto get_field_name_str_view() {
const auto func_name =
std::string_view{std::source_location::current().function_name()};
return func_name;
}
const int y = 0;
int main(int argc, const char** argv) {
constexpr const int* x = &y;
std::cout << get_field_name_str_view<test, x>() << '\n';
return 0;
}
I just checked and I get the same result building with the standard windows clang++.exe
17.0.6 downloaded from the LLVM releases page, so I think the problem is not exclusive to clang-cl
, rather clang on windows generally.
It seems that __PRETTY_FUNCTION__
does work as expected in the above test, i.e. I get auto get_field_name_str_view() [T = test, ptr = &y]
as (presumably) expected.
I can put up a PR to use that instead when __clang__
and _MSC_VER
are both defined, if you'd be willing to accept it? I know the docs say "Standard C++ only, no compiler-specific macros", but as far as I see the implementation of get_field_name_str_view
and similar functions is already not really standard C++ by depending on the exact format of implementation-defined strings.
Yes, I would accept the PR. Thank you so much for flagging this and for coming up with a fix.
The following snippet fails to compile under
clang-cl
version 17.0.6 and Visual Studio 17.8.5 with/std:c++latest
.The same snippet compiles successfully when building with the microsoft compiler, or if I remove one of the fields in the struct.
The compilation error seems to be some sort of static_assert about duplicate string literals: