...">

Explicit operator << selects "incorrect" overload

I play with variable templates and wrote this based on this answer :

template <size_t... I>
void print(seq<I...>)
{
    decltype(std::cout) * dummy[sizeof...(I)] = { &(std::cout << I << ' ')... };
}

Since it std::cout::operator<<has a return type, it can be saved, so there is no need for a trick ( ,0).

Now, to close the warning "unused variable 'dummy'" and print a new line, I tried the following instructions, but they did not do what I wanted:

dummy[0]->operator <<('\n'); // prints 10

(apparently called operator<<(int)insteadoperator<<(char)

dummy[0]->operator <<("\n"); // prints a pointer

(apparently called operator<<(const void*)insteadoperator<<(const char*)

In the end, I had to write

*dummy[0] << '\n';             // prints a newline as desired

My question is: why were the "wrong" overloads chosen?

+4
source share
1

"" , std::ostream. char const char* std::ostream, ,

*dummy[0] << '\n';

operator<<(std::ostream&, char),

dummy[0]->operator <<('\n');

-, std::ostream::operator<<(int).

+9

Source: https://habr.com/ru/post/1612210/


All Articles