open-telemetry / opentelemetry-cpp

The OpenTelemetry C++ Client
https://opentelemetry.io/
Apache License 2.0
815 stars 391 forks source link

nostd/span.h seems to be not compatible with C++17 #2625

Closed Pravila00 closed 3 months ago

Pravila00 commented 3 months ago

Describe your environment

I am trying to use a logging exporter (user_events), as the other exporters, they require span either from STL, GSL or the backport (nostd/span.h)

Given that I am building with C++17, stl::span is not included, so it goes to use the nostd/span.h which fails with the following error when it is included in my application:

In file included from /home/pabloro/LocalApplication/out/vcpkg_linux/installed/x64-linux/include/opentelemetry/sdk/logs/exporter.h:9,
                 from /home/pabloro/LocalApplication/out/vcpkg_linux/installed/x64-linux/include/opentelemetry/exporters/user_events/logs/exporter.h:8,
                 from /home/pabloro/LocalApplication/logging/UserEventsLogEngine.cpp:13:
/home/pabloro/LocalApplication/out/vcpkg_linux/installed/x64-linux/include/opentelemetry/nostd/span.h:162:62: error: lambda-expression in template-argument only available with ‘-std=c++2a’ or ‘-std=gnu++2a’
  162 |                                     std::is_convertible<U (*)[], T (*)[]>::value>::type * = nullptr>
      |                                                              ^
/home/pabloro/LocalApplication/out/vcpkg_linux/installed/x64-linux/include/opentelemetry/nostd/span.h:162:64: error: expected ‘{’ before ‘,’ token
  162 |                                     std::is_convertible<U (*)[], T (*)[]>::value>::type * = nullptr>
      |                                                                ^
/home/pabloro/LocalApplication/out/vcpkg_linux/installed/x64-linux/include/opentelemetry/nostd/span.h:162:73: error: template argument 1 is invalid
  162 |                                     std::is_convertible<U (*)[], T (*)[]>::value>::type * = nullptr>
      |                                                                         ^
/home/pabloro/LocalApplication/out/vcpkg_linux/installed/x64-linux/include/opentelemetry/nostd/span.h:162:81: error: template argument 1 is invalid
  162 |                                     std::is_convertible<U (*)[], T (*)[]>::value>::type * = nullptr>
      |                                                                                 ^
/home/pabloro/LocalApplication/out/vcpkg_linux/installed/x64-linux/include/opentelemetry/nostd/span.h:162:82: error: ‘<declaration error>’ is not a template [-fpermissive]
  162 |                                     std::is_convertible<U (*)[], T (*)[]>::value>::type * = nullptr>
      |                                                                                  ^~
/home/pabloro/LocalApplication/out/vcpkg_linux/installed/x64-linux/include/opentelemetry/nostd/span.h:244:62: error: lambda-expression in template-argument only available with ‘-std=c++2a’ or ‘-std=gnu++2a’
  244 |             typename std::enable_if<std::is_convertible<U (*)[], T (*)[]>::value>::type * = nullptr>
      |                                                              ^
/home/pabloro/LocalApplication/out/vcpkg_linux/installed/x64-linux/include/opentelemetry/nostd/span.h:244:64: error: expected ‘{’ before ‘,’ token
  244 |             typename std::enable_if<std::is_convertible<U (*)[], T (*)[]>::value>::type * = nullptr>
      |                                                                ^
/home/pabloro/LocalApplication/out/vcpkg_linux/installed/x64-linux/include/opentelemetry/nostd/span.h:244:73: error: template argument 1 is invalid
  244 |             typename std::enable_if<std::is_convertible<U (*)[], T (*)[]>::value>::type * = nullptr>
      |                                                                         ^
/home/pabloro/LocalApplication/out/vcpkg_linux/installed/x64-linux/include/opentelemetry/nostd/span.h:244:81: error: template argument 1 is invalid
  244 |             typename std::enable_if<std::is_convertible<U (*)[], T (*)[]>::value>::type * = nullptr>
      |                                                                                 ^
/home/pabloro/LocalApplication/out/vcpkg_linux/installed/x64-linux/include/opentelemetry/nostd/span.h:244:82: error: ‘<declaration error>’ is not a template [-fpermissive]
  244 |             typename std::enable_if<std::is_convertible<U (*)[], T (*)[]>::value>::type * = nullptr>
      |                                                                                  ^~

First of all, it seems to complain about using a lambda-expression in a template argument, saying that this is only supported from C++20 onwards.

I guess the main idea of this span backport is to support previous C++ versions than 20, such as 11/14/17.

The only workaround I have found so far is using C++20 and std::span, but I break my application build when we use other C++ libraries that are not supported by C++20. Any idea of why this build is failing in my application?

Pravila00 commented 3 months ago

I have also seen this issue where the build fails with the same expression #2382 "Build failure in nostd/span.h"

marcalff commented 3 months ago

The opentelemetry-cpp CI covers C++17 already:

and even on the same ubuntu 20.04.

The compiler error message is misleading I think, it complains about lambda and therefore C++20 while the real issue is that the types used in std::is_convertible are not convertible.

Please paste the full build log for the file that fails to build, there must be a reference to which line of code triggered this, and I suspect this line of code assigns two nostd::span variables of incompatible types.

Pravila00 commented 3 months ago

Hi @marcalff

Thanks for the super fast answer, the line that triggers this in my code is: #include <opentelemetry/exporters/user_events/logs/exporter.h>

So basically I'm not trying to even initialize it yet, it fails when it includes the header file:

In file included from /home/pabloro/LocalApplication/out/vcpkg_linux/installed/x64-linux/include/opentelemetry/sdk/logs/exporter.h:9,
                 from /home/pabloro/LocalApplication/out/vcpkg_linux/installed/x64-linux/include/opentelemetry/exporters/user_events/logs/exporter.h:8,
                 from /home/pabloro/LocalApplication/Lib/logging/UserEventsLogEngine.cpp:13:
marcalff commented 3 months ago

I see, so some of the code comes from cpp-contrib:

The issue could be in opentelemetry-cpp, or in opentelemetry-cpp-contrib, to investigate.

Pravila00 commented 3 months ago

This should be independent of cpp-contrib, it should be happening with every opentelemetry-cpp logger exporter, as this include of nostd/span.h comes from: /home/pabloro/LocalApplication/out/vcpkg_linux/installed/x64-linux/include/opentelemetry/sdk/logs/exporter.h:9

marcalff commented 3 months ago

Can not reproduce.

#include <opentelemetry/exporters/user_events/logs/exporter.h>

int main(int, char*[])
{
  return 0;
}
export CPP_API_INC=/data/malff/CODE/MY_GITHUB/opentelemetry-cpp/api/include/
export CPP_SDK_INC=/data/malff/CODE/MY_GITHUB/opentelemetry-cpp/sdk/include/
export CPP_CONTRIB_INC=/data/malff/CODE/MY_GITHUB/opentelemetry-cpp-contrib/exporters/user_events/include/
export LINUX_TP1_INC=/data/malff/CODE/MY_GITHUB/opentelemetry-cpp-contrib/exporters/user_events/third_party/LinuxTracepoints/libeventheader-tracepoint/include
export LINUX_TP2_INC=/data/malff/CODE/MY_GITHUB/opentelemetry-cpp-contrib/exporters/user_events/third_party/LinuxTracepoints/libtracepoint/include

gcc -std=c++17 \
 -I${CPP_API_INC} \
 -I${CPP_SDK_INC} \
 -I${CPP_CONTRIB_INC} \
 -I${LINUX_TP1_INC} \
 -I${LINUX_TP2_INC} \
 -o main.o -c main.cc

This builds.

marcalff commented 3 months ago

Please take the scripts provided, adjust the include paths, and check if this build or not in your environment.

Pravila00 commented 3 months ago

Okay it seems my project was importing a different library, which was defining a macro called U and having a conflict on this template U definition, eventually I managed to make it compile.

Thanks a lot for your help on this @marcalff! I'm closing the issue as this is not relevant :)

joshdmcc commented 2 months ago

@Pravila00 What library are you using that caused this issue? I'm also having a similar issue on the same line: