template<typename T>
class so_5::outliving_reference_t< T >
Helper class for indication of long-lived reference via its type.
Sometimes it is necessary to store a reference to an object that lives longer that a reference holder. For example:
class config {...};
class config_consumer {
config & cfg_;
public :
config_consumer(config & cfg) : cfg_(cfg) {...}
...
};
void f() {
config cfg = load_config();
config_consumer consumer(cfg);
...
}
The problem there is: when we see consumer::consumer(cfg)
we can say is it safe to pass a reference to short-lived object to it or not. Helper class outliving_reference_t can be used as indicator that consumer::consumer
expects a reference to long-lived object:
class config_consumer {
public :
: cfg_(cfg)
{...}
...
};
void f() {
config cfg = load_config();
...
}
If it is necessary to store a const reference to long-lived object then outliving_reference_t<const T> and outliving_const() should be used:
class data_processor {
public :
: cfg_(cfg)
{...}
...
};
void f() {
config cfg = load_config();
...
}
- Attention
- outliving_reference_t has no copy operator! It is CopyConstructible, but not CopyAssingable class.
- Template Parameters
-
- Since
- v.5.5.19