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

Compile error under MSVC 2015/2017 if <thread> included in same file as "doctest.h" #72

Closed
lewissbaker opened this issue May 21, 2017 · 0 comments

Comments

@lewissbaker
Copy link

lewissbaker commented May 21, 2017

The following snippet fails to compile with Visual Studio 2017.1 and single-header doctest.h from v1.2.0.
I also see the same compiler error under VS 2015.3.

#include <thread>
#include "doctest.h"

It fails with the following compile error:

c:\path\to\doctest.h(830): error C2248: 'std::thread::id::id': cannot access private member declared in class 'std::thread::id'
c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.25017\include\thread(165): note: see declaration of 'std::thread::id::id'
c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.25017\include\thread(149): note: see declaration of 'std::thread::id'
c:\path\to\doctest.h(836): note: see reference to class template instantiation 'doctest::detail::has_insertion_operator_impl::has_insertion_operator<T>' being compiled
        with
        [
            T=char
        ]
c:\path\to\doctest.h(1828): note: see reference to class template instantiation 'doctest::detail::has_insertion_operator<T>' being compiled
        with
        [
            T=char
        ]
c:\path\to\doctest.h(1833): note: see reference to class template instantiation 'doctest::detail::StringStream<T>' being compiled
        with
        [
            T=char
        ]
c:\path\to\doctest.h(1863): note: see reference to function template instantiation 'void doctest::detail::toStream<T>(std::ostream *,const T &)' being compiled
        with
        [
            T=char
        ]
c:\path\to\doctest.h(1862): note: while compiling class template member function 'void doctest::detail::ContextBuilder::Capture<char>::toStream(std::ostream *) const'
c:\path\to\doctest.h(1869): note: see reference to class template instantiation 'doctest::detail::ContextBuilder::Capture<char>' being compiled

However, if I add #include <ostream> before including "doctest.h" it compiles fine.

It seems like it's missing the declaration of operator<<(std::ostream&, char) from <ostream>.

This means that when it's instantiating doctest::detail::has_insertion_operator<char> it's only finding the operator<<(std::ostream&, std::thread::id) overload and thus trying to convert the char value to a std::thread::id via the private constructor, which takes an unsigned int.

The doctest.h header includes standard headers inside a section guarded by #if defined(DOCTEST_CONFIG_IMPLEMENT) || !defined(DOCTEST_SINGLE_HEADER).

This means that if you're using the single-header version then unless you have defined either the DOCTEST_CONFIG_IMPLEMENTATION_IN_DLL or the DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN macro then doctest.h won't pull in any of the standard headers that define the built-in ostream operators.

I think doctest.h needs to #include <ostream> to get visibility of the std::ostream& operator<<(std::ostream&, char) overload as it's required by the ContextBuilder::Chunk type, which instantiates Capture<char>, which instantiates detail::toStream<T>(std::ostream&, const char&), which instantiates detail::StringStream<char>, which instantiates detail::has_insertion_operator<char>, which tries to sink a char value into a std::ostream.

Or am I missing something in how I'm using the library?

I've tried defining DOCTEST_CONFIG_USE_IOSFWD but this doesn't fix the problem (<iosfwd> doesn't declare operator<< overloads under MSVC).

@onqtam onqtam closed this as completed in 777dddb May 24, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant