Visual C++ RTM
"To increase maintainability, reduce the risk of subtle errors in complex program code, and to make the use of initializes more consistent."
- Michael Spertus and Bill Seymour
Non Static Data Member initializers
class widget
{
int a = 42;
string b = “xyzzy”;
vector<int> c = { 1, 2, 3, 4 };
public:
widget()
{
}
// 42 xyzzy 1 2 3 4
explicit widget(int val) : a{val}
{
}
// val xyzzy 1 2 3 4
widget(int i, int j) : c{i, i, i, i, j, j}
{
}
// 42 xyzzy i i i i j j
};
Functionalities | C++ RTM
1. DEFAULT | =default
“The definition form "=default;" indicates that the function’s default definition should be used.”
- Lawrence Crowl
Attributes of =default
Why is =default better than writing it yourself?
- =default is shorter and less redundant.
- Default implementations can be more efficient than manual implementations.
- Default implementations can be “trivial,” so preserve POD-ness.
- Provides an easy way to get an otherwise implicitly-suppressed generated function.
Example
class value
{
//possibly lots of members
public:
value& operator=( const value& );
// provide custom
value( const value& );
// copying behavior
value() = default;
// un-suppress default constructor w/o rewriting it
};
Part of another feature: Will be added when we support implicit move generation.
2. DELETE | =delete
“Use of default language facilities [and problematic conversions] can be made an error by deleting the definition… [This] achieves the goal of making a bad overload visible.”
- Lawrence Crowl
Example
- Disabling copying
- Better diagnostic
class type
{
public:
type( const type& ) = delete;
type& operator=( const type& ) = delete;
type() = default;
// restore suppressed default constructor
};
Example: The following is an examle of eliminating undesirable conversions/overloads.
void bar( long long );
// accept long long…
void bar( long ) = delete;
// but nothing else
class custom_regex_iterator
{
// stores a pointer to its regex
public:
custom_regex_iterator( const regex& );
// lvalues only please
custom_regex_iterator( const regex&& ) = delete;
// no rvalues need apply
};