sharkdp/dbg-macro

`Type does not support the << ostream operator` on custom type

Aincvy opened this issue · 10 comments

Here is the code.

// a.h
#include "imgui.h"

std::ostream& operator<<(std::ostream& out, const ImVec2& v);
// a.cpp
std::ostream& operator<<(std::ostream& out, const ImVec2& v) {
      out << v.x << ", " << v.y;
      return out;
}
// main.cpp
#include <iostream>
#include "imgui.h"
#include "a.h"
#include "dbg.h"

void test (){
    auto pos = ImVec2(1,2);
    std::cout << pos;   // it's ok
    dbg(pos);    //  Type does not support the << ostream operator
}

Thank you for reporting this. Can you provide an example that I could reproduce (without dependencies on external libraries)?

Strange, I cann't reproduce this in other project.

The thing that I don't understand is, std::cout << pos; works fine, but dbg(pos); not.

Here is the build error output.

[build] In file included from ../sight_external_widgets.cpp:8:
[build] In file included from ../dependencies/sight-util/sight_defines.h:11:
[build] ../dependencies/dbg-macro/dbg.h:443:3: error: static_assert failed due to requirement 'detail::has_ostream_operator<const ImVec2 &>::value' "Type does not support the << ostream operator"
[build]   static_assert(detail::has_ostream_operator<const T&>::value,
[build]   ^             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[build] ../dependencies/dbg-macro/dbg.h:452:3: note: in instantiation of function template specialization 'dbg::pretty_print<ImVec2>' requested here
[build]   pretty_print(stream, value,
[build]   ^
[build] ../dependencies/dbg-macro/dbg.h:773:38: note: in instantiation of function template specialization 'dbg::pretty_print<ImVec2>' requested here
[build]     const bool print_expr_and_type = pretty_print(stream_value, ref);
[build]                                      ^
[build] ../dependencies/dbg-macro/dbg.h:765:12: note: in instantiation of function template specialization 'dbg::DebugOutput::print_impl<ImVec2 &>' requested here
[build]     return print_impl(exprs.begin(), types.begin(), std::forward<T>(values)...);
[build]            ^
[build] ../sight_external_widgets.cpp:196:9: note: in instantiation of function template specialization 'dbg::DebugOutput::print<ImVec2 &>' requested here
[build]         dbg(pos);             //  Type does not support the << ostream operator
[build]         ^
[build] ../dependencies/dbg-macro/dbg.h:893:8: note: expanded from macro 'dbg'
[build]       .print({DBG_MAP(DBG_STRINGIFY, __VA_ARGS__)}, \
[build]        ^
[build] 1 error generated.
[build] ninja: build stopped: subcommand failed.
[build] Build finished with exit code 1

If you are still interested, you can check the code at https://github.com/Aincvy/sight.

Could you try moving the ostream operator definition into the global namespace?

I have tried this: move it into a namespace, and the error reproduces.

Here is the code file
test-code.zip
.

I am not sure, but I don't think this is good practice... shouldn't the ostream operator be defined in the global namespace?

I don't know....

I did not find too may things about that.It seems not a well-known rule.

Would you fix it or not?
If not, could you please add something into readme file?

see for example: https://stackoverflow.com/a/9230853/704831

If there is a clear guideline somewhere that it is OKAY to overload the operator within the user namespace, I'm inclined to fix this in dbg.h. Any help is appreciated.

I move the struct ImVec2 into the namespace sight, then it compiles fine.
Looks like the operator overloading function isn't found.
A bug in the compiler perhaps.

Using decltype fails with an operator using non-ADL lookup
Namespaces and Operator Overloading in C++

Ok, so you say this can be closed? Or has the bug not been fixed?

I think we can do nothing about that, close it.