Skip to content

Changed: make value public so it could be used as template parameter - #2

Open
RemRemRemRemRe wants to merge 5 commits into
FabienPean:masterfrom
RemRemRemRe:main
Open

Changed: make value public so it could be used as template parameter#2
RemRemRemRemRe wants to merge 5 commits into
FabienPean:masterfrom
RemRemRemRe:main

Conversation

@RemRemRemRemRe

Copy link
Copy Markdown

all base classes and non-static data members are public and non-mutable and

I want to find a way to implement "strong typed alias for enum class", and don't want the implicit conversion from the original enum class type. It's done with these two commits.

So that EYesOrNo could be used as EWantBreakfast, EWantLunch, EWantDinner in :

OrderMeal<EWantBreakfast::Yes, EWantLunch::No, EWantDinner::Yes>();

Ideally 👆

But in reality ;) The best I could only got ATM :

OrderMeal<EWantBreakfast{EYesOrNo::Yes}, EWantLunch{EYesOrNo::No}, EWantDinner{EYesOrNo::Yes}>();

BTW, why it don't have get to return reference to value? Is there any special consideration for it?

@RemRemRemRemRe

Copy link
Copy Markdown
Author

Thanks for this great repository!

@FabienPean

FabienPean commented Sep 7, 2025

Copy link
Copy Markdown
Owner

Hi there! I am glad you could find it useful. It was mostly an experiment to tailor an alias to a (my) desired behavior. It is not field-tested as I didn't have the opportunity to integrate it in a production grade software.

With regard to the changes, I understand the motivation to make it public, but the part around explicit constructor is not really in spirit of what I aimed. Generally if behavior must be tweaked, I'd recommend to go for more polished libs like the ones mentioned in the readme. If I understand correctly, you want the STRONG_ALIAS(EWantBreakfast) to not be constructible from EWantBreakfast but only from an enum of EYesOrNo?

I must admit I did not consider the case of enums at the time, only built-in types. It might require a dedicated alias class definition as enum has some different rules. Though each person might have different wish there, personally I would consider valid the construction from the original enum (but maybe not any other one)

Your behaviour may be obtained in some way without strong alias

enum class EEnabled{No,Yes};

constexpr int Yes = 1;
constexpr int No = 0;

template<EEnabled yn>
void foo(){}

int main()
{
    foo<EEnabled{Yes}>();
}

It does not have getter because the primarily goal was to rely on the underlying type capabilities, and builtin type are cheap to copy/build by value. The implicit conversion is not a reference because

// with: constexpr operator T&()       noexcept { return value; }
STRONG_ALIAS(A, int);
STRONG_ALIAS(B, int);  
{ A a; B b; a += b; } // would compile instead of being an error as currently

@RemRemRemRemRe

Copy link
Copy Markdown
Author

1

you want the STRONG_ALIAS(EWantBreakfast) to not be constructible from EWantBreakfast but only from an enum of EYesOrNo?

No, I don't. Sorry for didn't make it clear:

I want to use the specific alias type rather than the type being aliased or other alias type in some places.

for alias type :

STRONG_ALIAS(EWantBreakfast, EYesOrNo);
STRONG_ALIAS(EWantLunch, EYesOrNo);
STRONG_ALIAS(EWantDinner, EYesOrNo);

I want to do :

OrderMeal<EWantBreakfast::Yes, EWantLunch::No, EWantDinner::Yes>(); //

OrderMeal<EYesOrNo::Yes, EWantLunch::No, EWantDinner::Yes>(); // ❌ no implicit conversion from original type
OrderMeal<EWantLunch::Yes, EWantLunch::No, EWantDinner::Yes>(); // ❌ no implicit conversion from other alias type

RemHelperEnumAlias

2

Your behaviour may be obtained in some way without strong alias

Indeed, it's a less strict alternative, I would consider it. Thanks.

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

Successfully merging this pull request may close these issues.

2 participants