2

What was once known as std::experimental::optional is now known as std::optional in C++17.

But some libraries -- such as libpqxx -- haven't yet been updated to drop the experimental namespace.

So now with the new version of Ubuntu 18.10 which comes with g++ v8.2.0, attempting to compile projects that use libpqxx results in errors like this:

/usr/include/pqxx/internal/statement_parameters.hxx:213:13: error: ‘experimental’ in namespace ‘std’ does not name a type
  const std::experimental::optional<Arg> &arg)
             ^~~~~~~~~~~~
/usr/include/pqxx/internal/statement_parameters.hxx:213:35: error: expected unqualified-id before ‘<’ token
  const std::experimental::optional<Arg> &arg)
                                   ^
/usr/include/pqxx/internal/statement_parameters.hxx:213:35: error: expected ‘)’ before ‘<’ token
  const std::experimental::optional<Arg> &arg)
                                   ^
                                   )

Is there some sort of flag I can pass in to g++ so it defines that old experimental namespace?

These are the relevant version numbers in Ubuntu 18.10:

> dpkg -l | egrep "libpqxx|g\+\+"
ii  g++             4:8.2.0-1ubuntu1       amd64        GNU C++ compiler
ii  g++-8           8.2.0-7ubuntu1         amd64        GNU C++ compiler
ii  libpqxx-6.2     6.2.4-4                amd64        C++ library to connect to PostgreSQL
ii  libpqxx-dev     6.2.4-4                amd64        C++ library to connect to PostgreSQL (development files)
Barry
  • 286,269
  • 29
  • 621
  • 977
Stéphane
  • 19,459
  • 24
  • 95
  • 136
  • 2
    `experimental::optional` still exists in libstdc++, and is still in the `` header. Is that include just missing? – Barry Oct 22 '18 at 15:41
  • 3
    Looking at the [source code](https://github.com/jtv/libpqxx/blob/6.2.4/include/pqxx/internal/statement_parameters.hxx#L201-L208) it seems that libpqxx does try to use `std::optional` when possible, but apparently its build system misdetects this feature. The [release note](https://github.com/jtv/libpqxx/releases/tag/6.2.4) says you can fix the error by defining `PQXX_HIDE_EXP_OPTIONAL`. – cpplearner Oct 22 '18 at 16:53

1 Answers1

3

As @cpplearner pointed out in a comment above, libpqxx has a macro (PQXX_HIDE_EXP_OPTIONAL) you can define prior to including the pqxx header files. More of a temporary workaround than a solution, but in my case it allowed me to get past the error and working on the code I needed.

This is the definition I added to my cmake file:

ADD_DEFINITIONS ( -DPQXX_HIDE_EXP_OPTIONAL )
Stéphane
  • 19,459
  • 24
  • 95
  • 136