C2143 syntax error when including boost/optional.hpp
Asked Answered
Q

3

3

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"
Quincey answered 18/10, 2016 at 12:42 Comment(14)
Please edit your question with a minimal reproducible example or SSCCE (Short, Self Contained, Correct Example). Also post text, not pictures.Mahaliamahan
Show us the exact line that triggers the error, and please edit your question to include the code as text, not screenshots. Also, std::binary_function is deprecated.Winnipegosis
In addition to "stop posting pictures, I can't copy/paste them to repro", in regards with "so there should be no problem with my Boost distribution as such.": be it as it may, it won't hurt to specify what version of boost you are using.Abbevillian
@Winnipegosis This deprecated usage comes from the Boost header, not from my code.Quincey
@AdrianColomitchi My boost version is 1.62.0, it's mentioned in the text.Quincey
Show us the source file where you include this header file.Douche
Note that you might get a better response on the boost mailing list.Douche
@MartinBonner Hi Martin, I added a very simple header and source example to the description, which already give me this error.Quincey
Bother. The usual reason for a weird compilation error in well-tested libraries like boost is a problem in an earlier header. Are you using precompiled headers?Douche
@MartinBonner No, I do not use precompiled headers as far as I know. I don't blame Boost, of course, it's surely my own problem. My question is - how to find out, which of my classes triggers the error? The only thing I see in the compiler output is this: D:\dev\lib\boost_1_62_0\boost/utility/compare_pointees.hpp(36): error C2143: syntax error: missing ',' before '<' 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 compiledQuincey
Is it possible that you are using /std:c++latest? I understand this switch removes some of the things that will be removed in c++17 and it's possible that Boost (or this particular Boost library) has not adapted yet to that change. If you are using it you should try /std:c++14 and see if it works for you.Malefic
@jv_ YES! That's it! I use /std:c++latest to enable C++17 one-line nested namespace definition. If I remove /std:c++latest, I can use boost::optional without any problems. Man, that was a great hint! Please enter it as solution and I will upvote it and mark as answer!Quincey
@jv_ Please enter your comment as answer to this question, because this was the solution for my problem. I will upvote it and mark it as accepted answer. Otherwise I will enter an answer myself, becasue this may be helpful to other people dealing with the same issue, Thanks!Quincey
Feel free to make an answer yourself, I'll upvote it.Malefic
Q
5

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.

Quincey answered 20/10, 2016 at 16:5 Comment(1)
I was also able to compile still using /std:c++latest by simply removing the inheritance from std::binary_function in the equal_pointees_t and less_pointees_t struct templates in my copy of compare_pointees.hpp. This may be a good workaround if you really want to use C++17 features before Boost removes the deprecated usage themselves.Resistive
L
4

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

Lugansk answered 30/12, 2016 at 12:31 Comment(3)
Yes, I can definitely confirm this statement. With boost 1.63.0 I can use the /std:c++latest switch along with boost/optional.hpp without any problems.Quincey
Unfortunately there's another use of std::unary_function in boost\algorithm\string\detail\util.hpp (boost version 1.64). :-(Coussoule
And there's also a use of std::unary_function and a use of std::binary_function in boost\icl\type_traits\predicate.hpp. (version 1.65)Nonessential
S
1

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.

Slaty answered 13/9, 2018 at 11:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.