Skip to content

pure virtual conversion operator #346

Description

@lzheng5

Consider the following code.

class ToJsonBase
{
protected:
  std::string b;

public:
  ToJsonBase(const std::string& ab) : b(ab){}

  virtual operator json() = 0;
};

class ToJson : public ToJsonBase
{
  std::string d;

public:
  ToJson(const std::string& ad)
    :ToJsonBase("Base String"), d(ad){}
  operator json()
  {
    json j;
    j["base"] = b;
    j["derived"] = d;
    return j;
  }
};

int main()
{
  ToJsonBase* p = new ToJson("derived string");
  json j = (json)*p; // error here
}

When you have the conversion operator as a pure virtual function, it will give me the following error.

In file included from /usr/include/c++/6.1.1/bits/uniform_int_dist.h:35:0,
                 from /usr/include/c++/6.1.1/bits/stl_algo.h:66,
                 from /usr/include/c++/6.1.1/algorithm:62,
                 from /home/lzheng5/Dropbox/home/ncsu/Spring2016/ECE484/IotFaceRec/./include/net/json.hpp:32,
                 from /home/lzheng5/Dropbox/home/ncsu/Spring2016/ECE484/IotFaceRec/tools/json-test/main.cpp:1:
/usr/include/c++/6.1.1/limits: In instantiation of ‘struct std::numeric_limits<ToJsonBase>’:
/home/lzheng5/Dropbox/home/ncsu/Spring2016/ECE484/IotFaceRec/./include/net/json.hpp:1418:94:   required by substitution of ‘template<class CompatibleNumberIntegerType, typename std::enable_if<((std::is_constructible<long long int, CompatibleNumberIntegerType>::value && std::numeric_limits<_Tp>::is_integer) && std::numeric_limits<_Tp>::is_signed), CompatibleNumberIntegerType>::type <anonymous> > nlohmann::basic_json<ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType>::basic_json(CompatibleNumberIntegerType) [with CompatibleNumberIntegerType = ToJsonBase; typename std::enable_if<((std::is_constructible<long long int, CompatibleNumberIntegerType>::value && std::numeric_limits<_Tp>::is_integer) && std::numeric_limits<_Tp>::is_signed), CompatibleNumberIntegerType>::type <anonymous> = <missing>]’
/home/lzheng5/Dropbox/home/ncsu/Spring2016/ECE484/IotFaceRec/tools/json-test/main.cpp:78:16:   required from here
/usr/include/c++/6.1.1/limits:320:7: error: invalid abstract return type ‘ToJsonBase’
       min() _GLIBCXX_USE_NOEXCEPT { return _Tp(); }
       ^~~
/home/lzheng5/Dropbox/home/ncsu/Spring2016/ECE484/IotFaceRec/tools/json-test/main.cpp:11:7: note:   because the following virtual functions are pure within ‘ToJsonBase’:
 class ToJsonBase
       ^~~~~~~~~~
/home/lzheng5/Dropbox/home/ncsu/Spring2016/ECE484/IotFaceRec/tools/json-test/main.cpp:19:11: note:  virtual ToJsonBase::operator nlohmann::json()
   virtual operator json() = 0;
           ^~~~~~~~
In file included from /usr/include/c++/6.1.1/bits/uniform_int_dist.h:35:0,
                 from /usr/include/c++/6.1.1/bits/stl_algo.h:66,
                 from /usr/include/c++/6.1.1/algorithm:62,
                 from /home/lzheng5/Dropbox/home/ncsu/Spring2016/ECE484/IotFaceRec/./include/net/json.hpp:32,
                 from /home/lzheng5/Dropbox/home/ncsu/Spring2016/ECE484/IotFaceRec/tools/json-test/main.cpp:1:
/usr/include/c++/6.1.1/limits:324:7: error: invalid abstract return type ‘ToJsonBase’
       max() _GLIBCXX_USE_NOEXCEPT { return _Tp(); }
       ^~~
/usr/include/c++/6.1.1/limits:330:7: error: invalid abstract return type ‘ToJsonBase’
       lowest() noexcept { return _Tp(); }
       ^~~~~~
/usr/include/c++/6.1.1/limits:336:7: error: invalid abstract return type ‘ToJsonBase’
       epsilon() _GLIBCXX_USE_NOEXCEPT { return _Tp(); }
       ^~~~~~~
/usr/include/c++/6.1.1/limits:340:7: error: invalid abstract return type ‘ToJsonBase’
       round_error() _GLIBCXX_USE_NOEXCEPT { return _Tp(); }
       ^~~~~~~~~~~
/usr/include/c++/6.1.1/limits:344:7: error: invalid abstract return type ‘ToJsonBase’
       infinity() _GLIBCXX_USE_NOEXCEPT { return _Tp(); }
       ^~~~~~~~
/usr/include/c++/6.1.1/limits:349:7: error: invalid abstract return type ‘ToJsonBase’
       quiet_NaN() _GLIBCXX_USE_NOEXCEPT { return _Tp(); }
       ^~~~~~~~~
/usr/include/c++/6.1.1/limits:354:7: error: invalid abstract return type ‘ToJsonBase’
       signaling_NaN() _GLIBCXX_USE_NOEXCEPT { return _Tp(); }
       ^~~~~~~~~~~~~
/usr/include/c++/6.1.1/limits:360:7: error: invalid abstract return type ‘ToJsonBase’
       denorm_min() _GLIBCXX_USE_NOEXCEPT { return _Tp(); }

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions