-->

C2143 syntax error when including boost/optional.h

2019-07-19 19:34发布

问题:

I'm stuck with a compile-time error which I cannot understand. I try to use boost::optional in my code, and as soon as I include boost/optional.hpp 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 boost::optional 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 boost/utility/compare_pointees.hpp (see GitHub link below). I also successfully use other stuff from Boost like boost::filesystem::path 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 and 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 boost::optional<size_t> as return type.

#pragma once

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

// C++ REST SDK
#define _TURN_OFF_PLATFORM_STRING
#include <cpprest/http_listener.h>
#include <cpprest/http_msg.h>

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

The place, where the compiler error is reported, is in the Boost header boost/utility/compare_pointees.hpp, line 36. You can view the full content of this file on GitHub under https://github.com/boostorg/utility/blob/boost-1.62.0/include/boost/utility/compare_pointees.hpp

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 Test.h:

#pragma once

#include <boost/optional.hpp>

Source file Test.cpp:

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

回答1:

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.



回答2:

The problem fixed in boost 1.63.0. It no longer uses std::binary_function which is removed in C++17.



回答3:

In my case I had a #define new DEBUG_NEW in the Force include file (C++->Advanced). I fixed it by adding a #undef new berore the boost #include's and then #define new DEBUG_NEW after.