Delegating ctors & reference vs rvalue reference

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
 
struct Test
{
    Test(std::istream &s)
    {
        int x;
        s >> x;
        std::cout << x << std::endl;
    }
    Test(std::istream &&s) : Test(static_cast<std::istream &>(s))
    {
    }
};
 
#include <sstream>
 
int main()
{
    Test t {std::istringstream("42")};
}
Is there a better way to do this than to use the cast? Is the way I am doing it currently wrong?
What makes you think you need a cast? Just : Test(s), it's an lvalue in that ctor.
The better way is to use forwarding.
Oh, hah. For some reason I thought that was infinite recursion. Thanks!
Topic archived. No new replies allowed.