Changed: make value public so it could be used as template parameter - #2
Changed: make value public so it could be used as template parameter#2RemRemRemRemRe wants to merge 5 commits into
Conversation
|
Thanks for this great repository! |
|
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 |
1
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 type2
Indeed, it's a less strict alternative, I would consider it. Thanks. |
I want to find a way to implement "strong typed alias for enum class", and
don'twant the implicit conversion from the original enum class type. It's done with these two commits.So that
EYesOrNocould be used asEWantBreakfast,EWantLunch,EWantDinnerin :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
getto return reference tovalue? Is there any special consideration for it?