Alatau Alatau - 1 year ago 162
C++ Question

C2143 syntax error when including boost/optional.hpp

I'm stuck with a compile-time error which I cannot understand. I try to use

in my code, and as soon as I include
I cannot build my project any longer. If I comment this include statement out, it works. I don't even have any actual usage of
in my code yet, just the include statement in the class header (see full header below). The compiler error is
C2143 syntax error: missing ',' before '<'
which happens in another Boost header
(see GitHub link below). I also successfully use other stuff from Boost like
in the same project already, so there should be no problem with my Boost distribution as such.

Here is my environment:
Microsoft Visual Studio Professional 2015 Version 14.0.25431.01 Update 3
boost 1.62.0
. I also use the third-party library C++ REST SDK, everything else is C++ standard library stuff.

My header looks like this. I want to add a new method with
as return type.

#pragma once

#include <boost/optional.hpp> // <==== ERROR

#include <cpprest/http_listener.h>
#include <cpprest/http_msg.h>

namespace SANDBOX::REST
class HttpGetHandler
virtual void HandleHttpGetRequest(web::http::http_request request) = 0;

The place, where the compiler error is reported, is in the Boost header
, line 36. You can view the full content of this file on GitHub under

The compiler output shows nothing more than these messages:

1>D:\dev\lib\boost_1_62_0\boost/utility/compare_pointees.hpp(36): error C2143: syntax error: missing ',' before '<'
1> D:\dev\lib\boost_1_62_0\boost/utility/compare_pointees.hpp(40): note: see reference to class template instantiation 'boost::equal_pointees_t<OptionalPointee>' being compiled
1>D:\dev\lib\boost_1_62_0\boost/utility/compare_pointees.hpp(59): error C2143: syntax error: missing ',' before '<'
1> D:\dev\lib\boost_1_62_0\boost/utility/compare_pointees.hpp(63): note: see reference to class template instantiation 'boost::less_pointees_t<OptionalPointee>' being compiled
========== Build: 0 succeeded, 1 failed, 2 up-to-date, 0 skipped ==========

It's surely not a problem of the Boost library. But how can I figure out, what's wrong with my classes or project settings?

P.S. I can reproduce the behavior even if I use these most primitive header and source file in my project:

Header file

#pragma once

#include <boost/optional.hpp>

Source file

#include "../include/Test.h"

Answer Source

I could figure out the reason due to a valuable hint by jv_. I turned on compiler switch /std:c++latest in my project settings to be able to use C++17 nested namespace definition feature. Activating this switch removes some deprecated language features, in particular std::binary_function, which is still in use in the current Boost distribution (1.62.0), hence producing the compiler error. Finally, I decided to remove the switch /std:c++latest (and use the ordinary way to declare my namespaces) and this solved the issue. Thank you all for helping me.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download