jll63 / yomm2

Fast, orthogonal, open multi-methods. Solve the Expression Problem in C++17.
Boost Software License 1.0
343 stars 18 forks source link

Error when compiling with raw non-const ptr #14

Closed cloudhan closed 3 years ago

cloudhan commented 3 years ago

with virtual_<matrix*> the following code

Click to expand! ```cpp #include #include #include using std::string; using yorel::yomm2::virtual_; struct matrix { virtual ~matrix() {} }; struct dense_matrix : matrix {}; struct diagonal_matrix : matrix {}; register_class(matrix); register_class(dense_matrix, matrix); register_class(diagonal_matrix, matrix); declare_method(string, to_json, (virtual_)); define_method(string, to_json, (dense_matrix* m)) { return "json for dense matrix ptr..."; } define_method(string, to_json, (diagonal_matrix* m)) { return "json for diagonal matrix ptr..."; } int main() { using std::cout; using std::cerr; yorel::yomm2::update_methods(); dense_matrix _a{}; diagonal_matrix _b{}; auto a = &_a; auto b = &_b; #pragma clang diagnostic ignored "-Wpotentially-evaluated-expression" cout << to_json(a) << "\n"; // json for dense matrix ptr cout << to_json(b) << "\n"; // json for diagonal matrix ptr return 0; } ```

triggers compiling error

D:\yomm2\examples\matrix.cpp(40,13): warning C4068: unknown pragma [D:\yomm2\build\examples\matrix.vcxproj]
D:\yomm2\include\yorel/yomm2.hpp(961,7): error C2665: 'yorel::yomm2::detail::resolver<1,yorel::yomm2::virtual_<matrix *>>::resolve': none of the 2 overloads could convert all the argument types [D:\yomm2\build\examples\matrix.vcxproj]
D:\yomm2\include\yorel/yomm2.hpp(710,18): message : could be 'void *yorel::yomm2::detail::resolver<1,yorel::yomm2::virtual_<matrix *>>::resolve(const yorel::yomm2::detail::word *,uintptr_t,size_t,const yorel::yomm2::detail::word *,const T &)' [D:\yomm2\build\examples\matrix.vcxproj]
          with
          [
              T=matrix *
          ]
D:\yomm2\include\yorel/yomm2.hpp(692,18): message : or       'void *yorel::yomm2::detail::resolver<1,yorel::yomm2::virtual_<matrix *>>::resolve(const yorel::yomm2::detail::word *,uintptr_t,size_t,yorel::yomm2::detail::word,const T &)' [D:\yomm2\build\examples\matrix.vcxproj]
          with
          [
              T=matrix *
          ]
D:\yomm2\include\yorel/yomm2.hpp(959,1): message : while trying to match the argument list '(yorel::yomm2::detail::word *, uintptr_t, size_t, yorel::yomm2::detail::word, const T *)' [D:\yomm2\build\examples\matrix.vcxproj]
          with
          [
              T=matrix
          ]
D:\yomm2\include\yorel/yomm2.hpp(959): message : while compiling class template member function 'void *yorel::yomm2::detail::method<_yomm2_method_to_json,std::string (yorel::yomm2::virtual_<matrix *>),yorel::yomm2::default_policy>::resolve(yorel::yomm2::policy::hash_factors_in_globals,const T *)' [D:\yomm2\build\examples\matrix.vcxproj]
          with
          [
              T=matrix
          ]
D:\yomm2\include\yorel/yomm2.hpp(955): message : see reference to function template instantiation 'void *yorel::yomm2::detail::method<_yomm2_method_to_json,std::string (yorel::yomm2::virtual_<matrix *>),yorel::yomm2::default_policy>::resolve(yorel::yomm2::policy::hash_factors_in_globals,const T *)' being compiled [D:\yomm2\build\examples\matrix.vcxproj]
          with
          [
              T=matrix
          ]
D:\yomm2\examples\matrix.cpp(19): message : see reference to class template instantiation 'yorel::yomm2::detail::method<_yomm2_method_to_json,std::string (yorel::yomm2::virtual_<matrix *>),yorel::yomm2::default_policy>' being compiled [D:\yomm2\build\examples\matrix.vcxproj]

With const matrix* const dense_matrix* and const diagonal_matrix* all work fine.

cloudhan commented 3 years ago

compiler version

Microsoft (R) C/C++ Optimizing Compiler Version 19.26.28806 for x64
Copyright (C) Microsoft Corporation.  All rights reserved.
derpda commented 3 years ago

My guess is that YOMM2 is intended for value semantics (which are preferred in general). I will leave the explanation of the specific error to @jll63 since I still don't understand yomm well enough.

From the example it is not clear that pointer semantics are needed. Might this be a classical XY problem? Why don't you just pass a reference instead? Not sure if I am being helpful :D

jll63 commented 3 years ago

This is indeed a bug. I just pushed a fix. Thanks for the report.