Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Compiling failed with mscv 19.34 #52

Closed
illegal-instruction-co opened this issue Jan 4, 2023 · 7 comments
Closed

Compiling failed with mscv 19.34 #52

illegal-instruction-co opened this issue Jan 4, 2023 · 7 comments
Assignees

Comments

@illegal-instruction-co
Copy link

illegal-instruction-co commented Jan 4, 2023

Stack:
C:\Users\PC\vcpkg\installed\x64-windows\include\nameof.hpp(956.19): message : see reference to variable template 'const auto static_v<nameof::detail::union_type >' compiled
1>C:\Users\PC\vcpkg\installed\x64-windows\include\nameof.hpp(961,1): message : compiled template function 'auto nameof::detail::get_member_name<pointer-to-member(0x10) See reference for creating )>(void) noexcept' instance
1>C:\Users\PC\vcpkg\installed\x64-windows\include\nameof.hpp(1098,40): message : see reference to compiled variable template 'const auto member_name_v<16>'
1>C:\Users\PC\Desktop\cartelsol\moscam-be\Entity\include\Entity/Spouse.h(28,41): message : compiled template function 'std::basic_string_view<char,std::char_traits< See reference to create char>> nameof::nameof_member<pointer-to-member(0x10)>(void) noexcept' instance
1>C:\Users\PC\vcpkg\installed\x64-windows\include\nameof.hpp(956.69): error C2296: '.*': not valid because left operand has type 'const int'
1>C:\Users\PC\vcpkg\installed\x64-windows\include\nameof.hpp(1099.29): error C2338: static_assert failed: 'Member does not have a name.'

@illegal-instruction-co illegal-instruction-co changed the title Compiling Fault Compiling failed with mscv 19.34 Jan 4, 2023
@schaumb
Copy link
Contributor

schaumb commented Jan 17, 2023

Hi @illegal-instruction-co,

I tried to reproduce it, but I could not able.

Can you share a minimal example that caused this issue?

@illegal-instruction-co
Copy link
Author

illegal-instruction-co commented Jan 26, 2023

#include "https://raw.githubusercontent.com/Neargye/nameof/master/include/nameof.hpp"

#include <string>
#include <iostream>

using namespace std;

struct X {
	X(string aThing) noexcept : aThing(move(aThing)) {
	}

	string aThing;

	// static constexpr auto what = NAMEOF(&X::aThing); ----> Works as expected

	static constexpr auto what2 = NAMEOF_MEMBER(&X::aThing);
};


int main() {
	auto xObj = X("test");

	// cout << xObj.what << endl; ----> Works as expected

	cout << xObj.what2 << endl;

	return 0;
}

@illegal-instruction-co
Copy link
Author

illegal-instruction-co commented Jan 26, 2023

Its working cool with gcc as we can see:
https://godbolt.org/z/GGneajPGP

With msvc:
https://godbolt.org/z/qG17bE9vc

@schaumb
Copy link
Contributor

schaumb commented Feb 20, 2023

Thanks, @illegal-instruction-co

So the main problem here is that the code tries to reach the class definition while initializing the constexpr static variable with the member's name.
This is not work in MSVC because its implementation uses the class member through the class.
I linking the #47 issue where this restriction needs to be mentioned.

Note for the developers:
The current workaround is that It needs to be separate the definition and the declaration

struct X {
   int member;
   static constexpr auto name = NAMEOF_MEMBER(&X::member); // BAD
};


struct X {
   int member;
   static const std::string_view name;
};
inline constexpr std::string_view X::name = NAMEOF_MEMBER(&X::member);

Note for the lib:
When NAMEOF_MEMBER macro is used, it is probably enough to call the MEMBER macro with a static assert.

@Neargye Neargye self-assigned this Feb 21, 2023
@Neargye
Copy link
Owner

Neargye commented Feb 21, 2023

Will add this to doc

@illegal-instruction-co
Copy link
Author

struct X {
int member;
static constexpr auto name = NAMEOF_MEMBER(&X::member); // BAD
};

struct X {
int member;
static const std::string_view name;
};
inline constexpr std::string_view X::name = NAMEOF_MEMBER(&X::member);

constexpr already put forward inline declaration, and why should i descript X::name out of struct ? I guess using NAMEOF macro is better approach

@schaumb
Copy link
Contributor

schaumb commented Mar 6, 2023

I agree the NAMEOF is easier to use in this situation.

Just a side note for this issue:

The main difference between NAMEOF and NAMEOF_MEMBER:

#define ATHING &X::aThing
struct X {
	string aThing;
	static constexpr auto aThingPtr = &X::aThing;
	static constexpr auto what = NAMEOF(aThingPtr);
	static constexpr auto what1 = NAMEOF(ATHING);
	static const std::string_view what2;
	static const std::string_view what3;
};
inline constexpr std::string_view X::what2 = NAMEOF_MEMBER(X::aThingPtr);
inline constexpr std::string_view X::what3 = NAMEOF_MEMBER(ATHING);

static_assert(X::what == "aThingPtr");
static_assert(X::what1 == "ATHING");
static_assert(X::what2 == "aThing");
static_assert(X::what3 == "aThing");

The NAMEOF macro will use stringizing operator on the argument as is and extracts the name from that.
The NAMEOF_MEMBER macro will resolve the argument value and extract the name with a notable hack I found.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants