C++ Removed Features

Animesh - Jul 2 '21 - - Dev Community

With the new version of C++17 many new functions introduced but some features are removed or deprecated.😊

These are listed below :

  • Removal of deprecated operator ++
  • Removal of registers
  • Removal of auto_ptr
  • Trigraphs
  • throw(typeid)
  • Allocator support in std::function
  • std::pointer_to_unary_function and std::pointer_to_binary_function
  • std::binder1st and std::binder2nd
  • std::bind1st and std::bind2nd
  • Other functions

1. REMOVAL OF DEPRECATED OPERATOR ++

Postfix and prefix Increment (++) expressions are now not valid for bool operands, since pre-fix and post-fix operator++ was overloaded for type bool but In both cases, the return value for a bool argument is true. The bool type does not support the full set of arithmetic types. Since from the launch of C++98 this change was much awaiting. In new version of C++17 it is no longer considered an arithmetic type and these operators have been deprecated.

Alternatives :
std::exchange can be used as alternative for this, but only where post-fix operator has valid uses. Exchange function replaces the value of object with new value and returns the old value of object.

2. REMOVAL OF REGISTERS

Long back, in C++11 register keyword was deprecated. Register keyword specifies or gives a hint to compiler that the variable can be put in a register for fast access or these variables might be heavily used so that it can do optimization by storing it in a CPU register. But compiler implicit do optimizations and hint was rarely used. Therefore in new version register keyword is removed, although keyword is still reserved for future versions.

syntax :

register string s="Bye Bye register :)"
Enter fullscreen mode Exit fullscreen mode

Alternatives :

There is no alternative for register as compiler does the same work automatically.

3. REMOVAL OF AUTO_PTR

auto_ptr was used to create a smart pointer to handle an object’s lifetime. It is owner of the object to which it refers, when an object gets destroyed auto_ptr also gets destroyed automatically. This smart pointer quietly steals ownership of the managed object in its copy constructor and copy assignment from the right-hand argument. As a result, the copy is not the same as the original smart pointer object. Because of these copy semantics, auto_ptr not works as CopyConstructible, and therefore it is deprecated.

Alternatives :
auto_ptr can easily replaced by unique_ptr which is also a smart pointer with similar working but with improved security. It was introduced in C++11 as direct replacement of auto_ptr as it provide new features (deleters) and support for arrays moreover it allows only one owner of the referencing pointer. So, while using unique_ptr there can only be at most one unique_ptr for one resource and when it is destroyed, the resource is automatically claimed. If we try to make a copy of unique_ptr will cause a compile-time error.

Example :

 unique_ptr<T> p1 (new T);
 unique_ptr<T> p2 = p1; 
 // Error: can't copy unique_ptr
Enter fullscreen mode Exit fullscreen mode

4. TRIGRAPHS

Trigraphs are group of three character basically, it is special character sequence which is used as alternative for some characters. Represented with two question marks.

example :
??- produces ~
??= produces #
??/ produces \
??’ produces ^
??( produces [
??) produces ]
??! produces |
??< produces {
??> produces }

But they produce much confusion as they are parsed before comments and therefor removed in the latest version.

Alternatives :
C++17 not provide any alternatives for Trigraphs, as modern keyboards have all this features moreover it produces a lot of bugs in code.
C
5. throw(typeid)

If a function is declared with type T listed in its exception specification, the function may throw exceptions of that type or a type derived from it. This is the non-throwing version of the dynamic exception specification hat has been deprecated and now removed. It has been replaced with noexcept which has more clear meaning.

Syntax :
throw(typeid, typeid, ...)
example :

void throwsInt(int x) throw(int) 
{  
    cout<<"throw function replaced with noexcept :)";  
    if (x == 0) 
    { 
      throw 1;  
    }
}  
Enter fullscreen mode Exit fullscreen mode

Alternatives :
As mentioned above, throw can have better alternative with noexcept. It specifies whether function could throw exceptions or not without specifying their type. But use it only when invocation of the function cannot throw any error, otherwise program will terminate.

6. ALLOCATOR SUPPORT IN std::function :

Several constructors allow to specify an allocator used for allocating internal memory. std::function also have constructors that take an allocator argument, but the semantics are unclear, and there are technical glitch with storing an allocator in a type-erased context and then recovering that allocator later for any allocations needed during copy assignment. Therefore, these constructor overloads were removed in C++17.

Alternatives :
No such features is present in C++ which replaces allocator.

7. std::pointer_to_unary_function, std::pointer_to_binary_function :

std::pointer_to_unary_function, std::pointer_to_binary_function function objects that act as wrappers around unary or binary functions. These functions contains constructor which constructs a new pointer_to_unary_function object with the supplied function and operator() which calls the stored function.

Alternatives :
These two functions std::function and std::ref replaces std::pointer_to_unary_function, std::pointer_to_binary_function.

8. std::binder1st and std::binder2nd :

These are function object that binds an argument to a binary function.
The value of the parameter is passed to the object at the construction time and stored within the object. Whenever the function object is invoked though operator(), the stored value is passed as one of the arguments, the other argument is passed as an argument of operator(). The resulting function object is an unary function.

1) binder1st : Binds the first parameter to the value value given at the construction of the object.

2) binder2nd : Binds the second parameter to the value value given at the construction of the object.

Alternatives :

Lambdas, std::bind are two features which can be alternatives for binder1st and binder2nd.

9. std::bind1st and std::bind2nd :

These are Helper functions that create instances of std::binder1st or std::binder2nd, which binds a given argument to a first or second parameter of a given binary function object. But these become no use with the introduction of lambdas in C++11, therefor they were deprecated.

10 Other functions :

  • std::mem_fun_t
  • std::mem_fun1_t
  • std::const_mem_fun_t
  • std::const_mem_fun1_t
  • std::mem_fun_ref_t
  • std::mem_fun1_ref_t
  • std::const_mem_fun_ref_t
  • std::const_mem_fun1_ref_t

These are function objects that wrap a pointer to a member function with no parameters or one parameter. The class instance whose member function to call is passed as a pointer to the operator() ie, the object whose member function to call is passed by pointer to the call operator for the latter, it is passed as a reference. They are deprecated because they are limited to member functions with either none or just one argument and you need different functions and function objects for handling pointers or references to the class instance.

Alternatives :
Alternative to above functions is std::mem_fn which can handle member functions with any number of variables and not only references or pointers to objects but also smart pointers.
C
Conclusion :

C++ is simple, much clear and provide faster programming. Improving such language with latest versions and removing old buggy features is necessary. Thousands of proposals were put forward for updating the features in C++17. And it's time to say bye to some old features. 😎

Connect
LinkedIn
Twitter

C++ Rocks!😄

. . .