Home > Net >  conversion operator overload : gcc vs clang problem
conversion operator overload : gcc vs clang problem

Time:10-05

I am trying to write a basic std::any alternative to use in my code, the reason is that i want to replace the templated std::any_cast with a conversion operator.

the use case is to add some introspection/reflection to structure and finally be able to ask a structure for its exposed members and access them by name ( as with a key in std::map ).

added functions to structure should be potentially virtual, templated class will not work.

the code actually look like this :

   // "std::any" like type that hold a "reference" to any type using a void*
   // -> keep track of original type using "std::type_info"
   // -> provide a "conversion operator" instead of "std::any_cast<T>" 
   // -> default initialized aRef object will only fail with std::bad_cast{}
  
   struct aRef {
       constexpr aRef() : ptr(nullptr), is_const(false), type(typeid(void)) { }

       template<typename T> explicit aRef(T* in) : ptr(in), is_const(false), type(typeid(T)) { }

       template<typename T> explicit aRef(const T* in) : ptr(const_cast<T*>(in)), is_const(true), type(typeid(T)) { }

       template<typename T> operator T&() {
          std::cout<<"operator T&() ";
          if (is_const) std::cout<<"----> make compiler issue an error !!! "<<std::endl;
          else          std::cout<<std::endl;
          if (typeid(T) == type) return *( static_cast<T*>(ptr) );
          else                   throw  std::bad_cast{};
       }

       template<typename T> operator const T&(){
          std::cout<<"operator const T&() "<<std::endl;
          if (typeid(T) == type) return *( static_cast<T*>(ptr) );
          else                   throw  std::bad_cast{};
       }


       private :
          void* const ptr;
          const bool is_const;
          std::type_info const& type;
   };

// what it should achieve : 
//       T& = aRef(      T*)   --> ok      
//       T& = aRef(const T*)   --> not ok, break constness 
//       T  = aRef(      T*)   --> ok      
//       T  = aRef(const T*)   --> ok    
// const T& = aRef(      T*)   --> ok   
// const T& = aRef(const T*)   --> ok   


// the structure to instrument
struct MDATA {
   double A;
   double B;

  // instrumentation code , functions can be polymorphic 
  // functions only -> zero overhead in size
   aRef operator[](const std::string&  key) {
      if      (key == "A")   return aRef(&A);
      else if (key == "B")   return aRef(&B);
      else                   return aRef();
   }

   aRef operator[](const std::string&  key) const {
      if      (key == "A")   return aRef(&A);
      else if (key == "B")   return aRef(&B);
      else                   return aRef();
   }

};

// dummy function 
void do_something(const double& x) { }

int main () {

   MDATA data {0.2,1.2};

// T& = aRef( T*)   --> ok      
{
  std::cout<<" T& = aRef( T*)... ";
  double& xx = data["A"];
   xx = 125.0; do_something(xx);
}

// T  = aRef( T*)   --> ok      
{
  std::cout<<" T = aRef( T*)... ";
   double xx = data["B"];
   xx = -521.0; do_something(xx);           // local !!
}

// const T& = aRef( T*)   --> ok    
{
  std::cout<<" const T& = aRef( T*)... ";
   const double& xx = data["A"];
   do_something(xx);
}

 const MDATA& cdata = data;

// T& = aRef( const T*)   --> not ok     
{
  std::cout<<" T& = aRef( const T*)... ";
   double& xx = cdata["A"];
   xx = 125.0; do_something(xx);
}

// T  = aRef(const T*)   --> ok      
{
  std::cout<<" T = aRef( const T*)... ";
   double xx = cdata["B"];
   xx = -521.0; do_something(xx);            // local !!
}

// const T& = aRef(const T*)   --> ok    
{
  std::cout<<" const T& = aRef( const T*)... ";
   const double& xx = cdata["A"];
   do_something(xx);
}

  return 0;

}

Now this is the result generated by gcc 11.1 :

|                             |                                                             |
| --------------------------- | ----------------------------------------------------------- |
|       T& = aRef( T*)        |  operator       T&()                                        |
|       T  = aRef( T*)        |  operator       T&()                                        |
| const T& = aRef( T*)        |  operator const T&()                                        |
|       T& = aRef( const T*)  |  operator       T&() ----> make compiler issue an error !!! |
|       T  = aRef( const T*)  |  operator       T&() ----> make compiler issue an error !!! |
| const T& = aRef( const T*)  |  operator const T&()                                        |

and the one generated by clang 11.1

|                             |                                                             |
| --------------------------- | ----------------------------------------------------------- |
|       T& = aRef( T*)        |  operator       T&()                                        |
|       T  = aRef( T*)        |  operator const T&()                                        |
| const T& = aRef( T*)        |  operator const T&()                                        |
|       T& = aRef( const T*)  |  operator       T&() ----> make compiler issue an error !!! |
|       T  = aRef( const T*)  |  operator const T&()                                        |
| const T& = aRef( const T*)  |  operator const T&()                                        |

As you can see , Clang give desired behavior and not Gcc, the proble is the call of conversion operator when assigning to value ( T = aRef(.) ) which is the const method for clang ( seems logical to me ) and other for Gcc.

So my questions :

  1. is this normal behavior or Gcc defect ???
  2. if this is normal, how can i manage to make Gcc get the wright overload ??

CodePudding user response:

You sample can be simplified to:

struct S
{
    int n1 = 1;
    const int n2 = 2;
    //template<typename T> operator T&() { return n1; }
    template<typename T> operator const T&() { return n2; }
};

int main()
{
    S s;
    int n = s;
    std::cout << n << std::endl;
}

gcc gives

error: cannot convert 'S' to 'int' in initialization

Demo

And according to Non-class_initialization_by_conversion

the non-explicit user-defined conversion functions of S and its base classes (unless hidden) that produce type T or a type convertible to T by a standard conversion sequence, or a reference to such type. cv qualifiers on the returned type are ignored for the purpose of selecting candidate functions.

As I understand, only operator int or int& should be considered.

so gcc would be right but on the non-template version Demo both are viable...

  • Related