Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pure virtual conversion operator #346

Closed
lzheng5 opened this issue Oct 29, 2016 · 4 comments
Closed

pure virtual conversion operator #346

lzheng5 opened this issue Oct 29, 2016 · 4 comments

Comments

@lzheng5
Copy link

lzheng5 commented Oct 29, 2016

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(); }
@nlohmann
Copy link
Owner

nlohmann commented Nov 2, 2016

I cannot reproduce the issue with Clang 3.5-3.8 as well as Apple's clang-800.0.42.1.
I can reproduce the issue with GCC 5.4.0 and GCC 6.2.0.

Not sure what to do about this.

@nlohmann nlohmann added confirmed state: help needed the issue needs help to proceed labels Nov 2, 2016
@boguscoder
Copy link

FYI, its interesting one however I could not solve it, so decided to ask here with minimal example http://stackoverflow.com/questions/40442956/clang-vs-gcc-in-abstract-class-handling-in-compile-time

@boguscoder
Copy link

it's not really json library issue at all, though it helps to surface the difference in compiler behavior

@nlohmann
Copy link
Owner

nlohmann commented Nov 8, 2016

Thanks for reporting and opening the thread at SO. There is already an answer, so I close this issue here. If anything related to the library comes up, please let me know.

@nlohmann nlohmann closed this as completed Nov 8, 2016
@nlohmann nlohmann removed the state: help needed the issue needs help to proceed label Nov 8, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants