Skip to main content
added 434 characters in body
Source Link
Draks
  • 323
  • 2
  • 9

I have had runtime error, when replaced some code by using std::optional:

Old code:

T getValue(); ... const auto& value = getValue(); value.get(); 

New code:

std::optional<T> getValue(); ... const auto& value = getValue().value(); value.get(); // Runtime error, crash 

It was unpredictable for me. The reason of crash is that the method returns T&&.

My question is in what cases T&& can be useful, why the method does not return a T.

Complete code:

#include <experimental/optional> #include <iostream> #include <memory> struct Value { std::unique_ptr<int> a = std::make_unique<int>(5); }; std::experimental::optional<Value> getValue() { Value v; return v; } int main() { const Value& value = getValue().value(); std::cout << *value.a << std::endl; return 0; } 

I have had runtime error, when replaced some code by using std::optional:

Old code:

T getValue(); ... const auto& value = getValue(); value.get(); 

New code:

std::optional<T> getValue(); ... const auto& value = getValue().value(); value.get(); // Runtime error, crash 

It was unpredictable for me. The reason of crash is that the method returns T&&.

My question is in what cases T&& can be useful, why the method does not return a T.

I have had runtime error, when replaced some code by using std::optional:

Old code:

T getValue(); ... const auto& value = getValue(); value.get(); 

New code:

std::optional<T> getValue(); ... const auto& value = getValue().value(); value.get(); // Runtime error, crash 

It was unpredictable for me. The reason of crash is that the method returns T&&.

My question is in what cases T&& can be useful, why the method does not return a T.

Complete code:

#include <experimental/optional> #include <iostream> #include <memory> struct Value { std::unique_ptr<int> a = std::make_unique<int>(5); }; std::experimental::optional<Value> getValue() { Value v; return v; } int main() { const Value& value = getValue().value(); std::cout << *value.a << std::endl; return 0; } 
added 3 characters in body
Source Link
Jarod42
  • 227.5k
  • 15
  • 214
  • 350

I have had runtime error, when replaced some code by using std::optional:

Old code:

T getValue(); ... const auto& value = getValue(); value.get(); 

New code:

std::optional<T> getValue(); ... const auto& value = getValue().value(); value.get(); // Runtime error, crash 

It was unpredictable for me. The reason of crash is that the method returns T&&T&&.

My question is in what cases T&&T&& can be usefulluseful, why the method does not return a TT.

I have had runtime error, when replaced some code by using std::optional:

Old code:

T getValue(); ... const auto& value = getValue(); value.get(); 

New code:

std::optional<T> getValue(); ... const auto& value = getValue().value(); value.get(); // Runtime error, crash 

It was unpredictable for me. The reason of crash is that the method returns T&&.

My question is in what cases T&& can be usefull, why the method does not return a T.

I have had runtime error, when replaced some code by using std::optional:

Old code:

T getValue(); ... const auto& value = getValue(); value.get(); 

New code:

std::optional<T> getValue(); ... const auto& value = getValue().value(); value.get(); // Runtime error, crash 

It was unpredictable for me. The reason of crash is that the method returns T&&.

My question is in what cases T&& can be useful, why the method does not return a T.

Source Link
Draks
  • 323
  • 2
  • 9

Why std::optional::value() &&; return &&?

I have had runtime error, when replaced some code by using std::optional:

Old code:

T getValue(); ... const auto& value = getValue(); value.get(); 

New code:

std::optional<T> getValue(); ... const auto& value = getValue().value(); value.get(); // Runtime error, crash 

It was unpredictable for me. The reason of crash is that the method returns T&&.

My question is in what cases T&& can be usefull, why the method does not return a T.