C++ operator== with shared_ptr and glm::vec3

213 views
Skip to first unread message

dejvu...@googlemail.com

unread,
Mar 4, 2017, 9:10:23 AM3/4/17
to ISO C++ Standard - Discussion
Hello I have a problem to compile the following code. It seems to me strange why the compiler does not accept my code:

bool operator==(const std::shared_ptr<glm::vec3> &v0, const glm::vec3 &v1) {
       
return *v0 == v1;
}

bool operator==(const glm::vec3 &v0, const std::shared_ptr<glm::vec3> &v1) {
       
return v0 == *v1;
}

TEST
(smartPointersAndOperators, OperatorEquals2) {
        std
::vector<std::shared_ptr<glm::vec3>> allAs {
                std
::make_shared<glm::vec3>(),
                std
::make_shared<glm::vec3>(),
                std
::make_shared<glm::vec3>()};
        glm
::vec3 obj;
       
auto it = std::find(allAs.cbegin(), allAs.cend(), obj);
}

The compiler output:
In file included from /home/dejo/Projekte/OpenGL/SansiEngine/test/C++11/c++11_Features_main.cpp:8:
In file included from /usr/include/gtest/gtest.h:55:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/../../../../include/c++/5.4.0/ostream:38:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/../../../../include/c++/5.4.0/ios:40:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/../../../../include/c++/5.4.0/bits/char_traits.h:39:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/../../../../include/c++/5.4.0/bits/stl_algobase.h:71:
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/../../../../include/c++/5.4.0/bits/predefined_ops.h:194:17: error: invalid operands to binary expression ('const std::shared_ptr<glm::tvec3<float,
      glm::precision::packed_highp> >'
and 'const glm::tvec3<float, glm::precision::packed_highp>')
       
{ return *__it == _M_value; }
                 
~~~~~ ^  ~~~~~~~~
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/../../../../include/c++/5.4.0/bits/stl_algo.h:120:8: note: in instantiation of function template specialization
     
'__gnu_cxx::__ops::_Iter_equals_val<const glm::tvec3<float, glm::precision::packed_highp> >::operator()<__gnu_cxx::__normal_iterator<const std::shared_ptr<glm::tvec3<float,
      glm::precision::packed_highp> > *, std::vector<std::shared_ptr<glm::tvec3<float, glm::precision::packed_highp> >, std::allocator<std::shared_ptr<glm::tvec3<float,
      glm::precision::packed_highp> > > > > >'
requested here
         
if (__pred(__first))
             
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/../../../../include/c++/5.4.0/bits/stl_algo.h:161:14: note: in instantiation of function template specialization
     
'std::__find_if<__gnu_cxx::__normal_iterator<const std::shared_ptr<glm::tvec3<float, glm::precision::packed_highp> > *, std::vector<std::shared_ptr<glm::tvec3<float,
      glm::precision::packed_highp> >, std::allocator<std::shared_ptr<glm::tvec3<float, glm::precision::packed_highp> > > > >, __gnu_cxx::__ops::_Iter_equals_val<const glm::tvec3<float,
      glm::precision::packed_highp> > >'
requested here
     
return __find_if(__first, __last, __pred,
             
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/../../../../include/c++/5.4.0/bits/stl_algo.h:3790:19: note: in instantiation of function template specialization
     
'std::__find_if<__gnu_cxx::__normal_iterator<const std::shared_ptr<glm::tvec3<float, glm::precision::packed_highp> > *, std::vector<std::shared_ptr<glm::tvec3<float,
      glm::precision::packed_highp> >, std::allocator<std::shared_ptr<glm::tvec3<float, glm::precision::packed_highp> > > > >, __gnu_cxx::__ops::_Iter_equals_val<const glm::tvec3<float,
      glm::precision::packed_highp> > >'
requested here
     
return std::__find_if(__first, __last,
                 
^
/home/dejo/Projekte/OpenGL/SansiEngine/test/C++11/templates/TemplateUnderTest.hpp:250:17: note: in instantiation of function template specialization
     
'std::find<__gnu_cxx::__normal_iterator<const std::shared_ptr<glm::tvec3<float, glm::precision::packed_highp> > *, std::vector<std::shared_ptr<glm::tvec3<float,
      glm::precision::packed_highp> >, std::allocator<std::shared_ptr<glm::tvec3<float, glm::precision::packed_highp> > > > >, glm::tvec3<float, glm::precision::packed_highp> >'
requested
      here
       
auto it = std::find(allAs.cbegin(), allAs.cend(), obj);

So I don't know, but the error does not make to  me any sense...
I hope  somebody can help me...

Daniel Krügler

unread,
Mar 4, 2017, 11:15:33 AM3/4/17
to std-dis...@isocpp.org
2017-03-04 15:10 GMT+01:00 dejvukadin via ISO C++ Standard -
Discussion <std-dis...@isocpp.org>:
> Hello I have a problem to compile the following code. It seems to me strange
> why the compiler does not accept my code:
>
> bool operator==(const std::shared_ptr<glm::vec3> &v0, const glm::vec3 &v1) {
> return *v0 == v1;
> }
>
> bool operator==(const glm::vec3 &v0, const std::shared_ptr<glm::vec3> &v1) {
> return v0 == *v1;
> }

Operators are expected to be found by ADL (argument-depending lookup).
To make this possible, operator== should be defined in the namespace
of one of the arguments. It is not valid to add functions to namespace
std (Except if they would be specializations of templates where at
least one type is a user-defined type), so namespace std is not
possible. The question is whether namespace glm is your own namespace.
If so, you should define operator== in that namespace. If that is not
true, I recommend to stay away of defining an operator== overload,
instead you should use the std::find_if which accepts as last argument
a binary predicate. Define a predicate (e.g. a lambda that corresponds
to the equality definition above and you are done. Note that your
predicate can define two operator() overloads that correspond to the
above shown operator== forms.

- Daniel

dejvu...@googlemail.com

unread,
Mar 4, 2017, 11:24:22 AM3/4/17
to ISO C++ Standard - Discussion
You are right!
Thank you a lot. I wrote that stuff in the glm namespace and it worked. Anyway I will consider the solution with find_if. I think, that is the better solution.

dejvu...@googlemail.com

unread,
Mar 4, 2017, 11:27:42 AM3/4/17
to ISO C++ Standard - Discussion
How do I mark the thread as solved?


Am Samstag, 4. März 2017 17:15:33 UTC+1 schrieb Daniel Krügler:

Daniel Krügler

unread,
Mar 4, 2017, 11:40:51 AM3/4/17
to std-dis...@isocpp.org
2017-03-04 17:27 GMT+01:00 dejvukadin via ISO C++ Standard -
Discussion <std-dis...@isocpp.org>:
> How do I mark the thread as solved?

This is not an "issue" list, so I don't think that you can add any
tags such as "Resolved" to it.

- Daniel

Tony V E

unread,
Mar 4, 2017, 3:21:50 PM3/4/17
to std-discussion
Furthermore, it probably wasn't the right place to ask the question in the first place, but luckily Daniel was nice enough to answer.
A better venue for future similar questions would probably be stackoverflow.com.
Good luck with your C++ing.


--
Be seeing you,
Tony
Reply all
Reply to author
Forward
0 new messages