Home Overloading templated variadic operator with rvalue reference
Reply: 0

Overloading templated variadic operator with rvalue reference

user1402
1#
user1402 Published in May 22, 2018, 9:21 pm

Good evening, ladies and gentlemen, here's my definition:

template<typename ... TL>
struct TemplatedType { };

template<typename ... TL>
std::istream& operator>>(std::istream& is, TemplatedType<TL ...> & sp)
{
    // do stuff

    return is;
}

And usage:

std::istringstream iss("I'd like some pancakes, please");
TemplatedType<int> a;
iss >> a;

And it works just fine, but I'd like to receive the templated arguments as r-value reference:

template<typename ... TL>
std::istream& operator>>(std::istream& is, const TemplatedType<TL && ...> & sp) {...}

And then, the compiler starts yelling:

C2678 binary '>>': no operator found which takes a left-hand operand of type 'std::istringstream' (or there is no acceptable conversion)

Where's the problem?


Motivation: I'd like to make a split function that can be used in this way:

std::istringstream iss("alpha:=10/50.1");

std::string x;
int y;
double z;

iss >> split(x, ':', '=', y, '/', z); // sets x=alpha, y=10, z=50.1

So the function needs to be able to receive both l-value and r-value references.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.314466 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO