Just like in the topic: Is there a way to create a user-defined-literal from std::initializer_list?
I am trying to do something like that:
template <typename T> inline
std::initializer_list<T> const & operator "" _lit(std::initializer_list<T> const & list)
{
return std::move(list); // I am not sure, but this line might cause undefined behavior... well I'll think about it latter...
}
int main()
{
{ 10, 20, 30, 40 }_lit // Error: identifier '_lit' is undefined;
return 0;
}
But it seems compiler doesn't understand that I am trying to call operator""_lit({10, 20, 30, 40}); Is there any way to fix it?
EDIT:
I am sorry, it turns out this is just another example of XY problem ...
Let me elaborate
I am trying to "expand" current C++ syntax (it's a fun little project...)
The main idea is to simplify this:
if ((val_1 == value) && (val_2 == value) && (val_3 == value)) { /* ... */ }
into somehting along the lines:
if (std::initializer_list<T>{val_1, val_2, val_3} == value)
ofc I am providing an additional operator:
template <typename T> inline
bool operator==(std::initializer_list<T> const & list, T const & ref)
{
for (auto const & element : list)
{
if (element == ref) { /* Do nothing. */ }
else
{
return false;
}
}
return true;
}
Everything would be fine and nice, but I dislike the need to type std::initializer_list<T> in front of a curly bracket... Otherwise, the compiler chooses the default version of operator==() and I get a compilation error...
Literals come here as an idea to change if (std::initializer_list<T>{val_1, val_2, val_3} == value) into if ({val_1, val_2, val_3}_lit == value)