B. A. Sylla
B. A. Sylla

Reputation: 409

reference to a value behind a pointer

I want a reference to a value behind a pointer.

class UnicastCall {
protected:
    std::fstream *m_stream_attachement_destination_;
...
public:
auto GetStreamAttachementDestination_AsPointer() -> decltype(m_stream_attachement_destination_)
    { return m_stream_attachement_destination_; } //THIS WORKS
auto GetStreamAttachementDestination_AsReference() -> decltype(*m_stream_attachement_destination_) &
    { return *m_stream_attachement_destination_; } //IS THIS CORRECT?
....
};

But I get an error.

error: use of deleted function 'std::basic_fstream<_CharT, _Traits>::basic_fstream(const std::basic_fstream<_CharT, _Traits>&) [with _CharT = char; _Traits = std::char_traits<char>]'
 auto fs = concrete_call->GetStreamAttachementDestination_AsReference();

Upvotes: 0

Views: 63

Answers (1)

Christian Hackl
Christian Hackl

Reputation: 27548

You are trying to copy an std::fstream, which is not allowed.

The error is not in your class but at the call site. auto fs = ... does not create a reference but attempts to call the copy constructor; the auto is a substitute only for std::fstream, not the &.

Try this instead:

auto& fs = concrete_call->GetStreamAttachementDestination_AsReference();

Upvotes: 2

Related Questions