1

I observed that if I explicitly delete only constructor and destructor of a class then the resultant implementation deletes copy constructor & move constructor, but the compiler still makes copy assignment and move assignment operators implicitly available! Which in turn makes assignment possible!

My question is what is the rational of this? What is the use case where this can be used. Following is an example code for reference:

# ifndef MOEGLICH_H_
# define MOEGLICH_H_

# include <cstdint>

class Moeglich final
{
public :

   explicit
   Moeglich()                                 = delete   ;
  ~Moeglich()                                 = delete   ;

/*
// With explicit deletion
   Moeglich& operator=(const Moeglich& other) = delete   ; 
   Moeglich(const Moeglich& other)            = delete   ;
   Moeglich&& operator=(Moeglich&& other)     = delete   ; 
   Moeglich(Moeglich&& other)                 = delete   ;
*/

   static constexpr uint16_t Egal(const uint8_t& var_) noexcept
   {
      return static_cast< uint16_t > ( var_ ) ;
   }

};

# endif

# include <cstdlib>
# include <iostream>
# include <type_traits>

int main(int argc, char* argv[])
{

   std::cout << std::boolalpha 
             << "Is constructible      : " << std::is_constructible<Moeglich>::value        << std::endl 
             << "Is destructible       : " << std::is_destructible<Moeglich>::value         << std::endl 
             << "Is copy constructible : " << std::is_copy_constructible<Moeglich>::value   << std::endl 
             << "Is move constructible : " << std::is_move_constructible<Moeglich>::value   << std::endl 
             << "Is copy assignable    : " << std::is_copy_assignable<Moeglich>::value      << std::endl 
             << "Is move assignable    : " << std::is_move_assignable<Moeglich>::value      << std::endl 
             << "Is assignable         : " << std::is_assignable<Moeglich, Moeglich>::value << std::endl 
              ;

/* Following were what I wanted to prevent anyway :

   const Moeglich mom {} ;
   Moeglich pop {} ;
   Moeglich foo {} ;
   foo = mom ;
   foo = std::move(pop) ;

*/

   return EXIT_SUCCESS ;

}

Edit:: I see I have created a lot of confusion by vaguely putting some codes and not mentioning intent. I will never construct this object. All I am interested is accessing

const uint8_t duh { 5 } ;
const uint16_t notEgal { Moeglich::Egal(duh) } ;

Here is what is important for me: Sometimes, I need partial template specialization of functions which is not allowed which can be enabled if I put this function inside a template class.

I have been pointed to a link here, which very clearly lays down the rule. My expectation from the compiler was wrong and my use-case cannot be understood in a special way by the compiler.

Thanks everybody for commenting.

Regards,

Sumit

  • You could reinterpret cast to it? – JVApen Jun 13 '18 at 16:20
  • "resultant implementation deletes copy constructor & move constructor" are you sure? Perhaps is_copy_constructible doesn't check exactly what you think it checks. If you want to prevent assignment, delete the assignment operator. – n. m. could be an AI Jun 13 '18 at 16:24
  • 1
    if you can't construct a object, how do you assign something to it? – Klaus Jun 13 '18 at 16:26
  • 2
    It should be noted that it is an open issue in C++ that [an aggregate can still be initialized with no values (through `{}`) even if it has a deleted default constructor](https://stackoverflow.com/q/23882409/734069). So you need to give the type a private member to prevent it from being an aggregate. – Nicol Bolas Jun 13 '18 at 16:27
  • @NicolBolas: by deleting the destructor, it is not longer possible to create an object, also not by aggregate initialization. – Klaus Jun 13 '18 at 16:31
  • @Klaus: It's not possible to *delete* the object. Creation has nothing to do with that. – Nicol Bolas Jun 13 '18 at 16:31
  • @NicolBolas: Theoretical yes. But every object will leave its scope where it was created. And this requires a destructor. By deleting the destructor the compiler complains about. As a result, you will be able to create, but you can not build your prog, because it fails by destruction. – Klaus Jun 13 '18 at 16:33
  • @Klaus you can always use `new`. – n. m. could be an AI Jun 14 '18 at 12:23

0 Answers0