Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

MSVC support #3

Open
HDAKFD opened this issue Aug 25, 2024 · 1 comment
Open

MSVC support #3

HDAKFD opened this issue Aug 25, 2024 · 1 comment

Comments

@HDAKFD
Copy link

HDAKFD commented Aug 25, 2024

Apparently this doesn't work on MSVC.

Did u test it on MSVC back then when you created this since now it does not work. I checked that COUNTER works just fine but the fameta::counter can't get past the start index with any given number.

MSVC:
image

LLVM Clang:
image

Code for that:
image

@mittorn
Copy link

mittorn commented Dec 26, 2024

I've immplemented same counter, but compatible with msvc.
It uses same method as unconstexpr-cpp20, but works in most of c++17 compilers (gcc7+, clang5+, all c++17 msvc versions)

template <typename T>
struct Counter
{
	template <int I>
	struct Flag
	{
		template<typename F>
		friend constexpr bool Check(Flag, F);
	};

	template<int I>
	struct MarkFlag
	{
		template<typename F>
		friend constexpr bool Check(Flag<I>, F) { return false; }
	};

	template <int I, typename F, bool = Check(Flag<I>{}, F{})>
	constexpr static bool IsMarked(int) { return true; }

	template <int I, typename F>
	constexpr static bool IsMarked(float) { return false; }

	template <typename F>  struct WrapT{};

	template<typename F, int I = 0>
	constexpr static  int Inc()
	{
		if constexpr (IsMarked<I, WrapT<F>>(0)) return Inc<F, I+1>();
		else return I + sizeof(MarkFlag<I>) - 1;
	};

};

// use lambda as unique type
#define CounterInc(T) []{constexpr auto unq = []{}; return Counter<T>::Inc<decltype(unq)>();}()

This does not generate warnings for non-template friend injection
I using lambda for unique identifier, this targets c++17. Maybe this will work on pre-c++17 compilers with unique types or line constants if replace Inc() by SFINAE
Main difference is passing unique type to friend injection template and splitting flag check from increment
I have not implemented binary search, but hope it's possible

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

No branches or pull requests

2 participants