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"
Software Craftsman
  • 2,999
  • 2
  • 31
  • 47
  • Please **[edit]** your question with a [mcve] or [SSCCE (Short, Self Contained, Correct Example)](http://sscce.org). Also post **text**, not pictures. – NathanOliver Oct 18 '16 at 12:49
  • 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. – SingerOfTheFall Oct 18 '16 at 12:49
  • 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. – Adrian Colomitchi Oct 18 '16 at 13:02
  • @SingerOfTheFall This deprecated usage comes from the Boost header, not from my code. – Software Craftsman Oct 18 '16 at 13:03
  • @AdrianColomitchi My boost version is 1.62.0, it's mentioned in the text. – Software Craftsman Oct 18 '16 at 13:05
  • Show us the source file where you include this header file. – Martin Bonner supports Monica Oct 18 '16 at 14:14
  • Note that you might get a better response on the boost mailing list. – Martin Bonner supports Monica Oct 18 '16 at 14:14
  • @MartinBonner Hi Martin, I added a very simple header and source example to the description, which already give me this error. – Software Craftsman Oct 18 '16 at 14:42
  • 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? – Martin Bonner supports Monica Oct 18 '16 at 14:47
  • @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' being compiled` – Software Craftsman Oct 18 '16 at 15:00
  • 3
    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. – llonesmiz Oct 19 '16 at 04:46
  • @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! – Software Craftsman Oct 19 '16 at 07:47
  • @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! – Software Craftsman Oct 20 '16 at 15:14
  • Feel free to make an answer yourself, I'll upvote it. – llonesmiz Oct 20 '16 at 15:40

3 Answers3

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.

Community
  • 1
  • 1
Software Craftsman
  • 2,999
  • 2
  • 31
  • 47
  • 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. – Jonathan Sharman Nov 21 '16 at 00:01
4

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

ash
  • 41
  • 2
  • 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. – Software Craftsman Jan 02 '17 at 13:45
  • Unfortunately there's another use of `std::unary_function` in boost\algorithm\string\detail\util.hpp (boost version 1.64). :-( – antred Jul 28 '17 at 22:35
  • 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) – vonludi Dec 12 '17 at 15:15
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.

Eusebiu Marcu
  • 320
  • 1
  • 10