abseil/abseil-cpp

[Bug]: absl/strings/internal/cord_rep_flat.h(42,39): error C2131: expression did not evaluate to a constant

Opened this issue · 3 comments

Describe the issue

cord_rep_flat.h does not compile with MSVC on Windows due to the offsetof macro definition in stddef.h:

// C:\Program Files (x86)\Windows Kits\10\Include\10.0.22621.0\ucrt\stddef.h
#if defined _MSC_VER && !defined _CRT_USE_BUILTIN_OFFSETOF
    #ifdef __cplusplus
        // This one is used in my setup
        #define offsetof(s,m) ((::size_t)&reinterpret_cast<char const volatile&>((((s*)0)->m)))
    #else
        #define offsetof(s,m) ((size_t)&(((s*)0)->m))
    #endif
#else
    #define offsetof(s,m) __builtin_offsetof(s,m)
#endif

Steps to reproduce the problem

// Anything that transitively includes <absl/strings/internal/cord_rep_flat.h>
#include <absl/container/flat_hash_map.h>

What version of Abseil are you using?

20230125.3 from conancenter

What operating system and version are you using?

Windows 11

What compiler and version are you using?

I think I'm using Microsoft (R) C/C++ Optimizing Compiler Version 19.37.32824 for x64 but MSBuild is inscrutable.

What build system are you using?

CMake 3.27.6

Additional context

No response

I would have thought you could disable this with -D _CRT_USE_BUILTIN_OFFSETOF but I get

warning C4117: macro name '_CRT_USE_BUILTIN_OFFSETOF' is reserved, '#define' ignored

Edit: nvm that wouldn't make sense

We need more information. We have an MSVC build but we don't see this error. Given the macro definition that you supplied above it isn't obvious why we don't see the error. Can you provide a minimal example, build commands, exact error messages, and/or build logs?

Sorry, you're right! Here's a more detailed repro. It looks a little involved, but as far as I can tell, this is the canonical way to use Conan.

I have the following Conan profile (created by conan profile --detect --name debug and changing build_type=Release to build_type=Debug:

arch=x86_64
build_type=Debug
compiler=msvc
compiler.cppstd=20
compiler.runtime=dynamic
compiler.version=193
os=Windows

Create a directory test with conanfile.txt:

[requires]
abseil/20230125.3

[generators]
CMakeDeps
CMakeToolchain

Run the following to install abseil via Conan:

test $ conan install . --build missing --output-folder conan-build-debug --profile debug

Create an example main.cpp script to compile:

#include <absl/containers/flat_hash_map.h>

int main() { return 0; }

A CMakeLists.txt:

cmake_minimum_required(VERSION 3.20)
set(CMAKE_CXX_STANDARD 20)
project(test)

add_executable(main main.cpp)

find_package(absl REQUIRED)
target_link_libraries(main abseil::abseil)
target_include_directories(main PRIVATE ${absl_INCLUDE_DIRS})

Run cmake in a build directory, e.g. cmake-build-debug. I've included the top of my output for reference:

test\cmake-build-debug $ cmake .. -DCMAKE_TOOLCHAIN_FILE=../conan-build-debug/conan_toolchain.cmake -DCMAKE_BUILD_TYPE=debug
-- Building for: Visual Studio 17 2022
-- Using Conan toolchain: E:/Repositories/noahbkim/test/conan-build-debug/conan_toolchain.cmake
-- Conan toolchain: C++ Standard 20 with extensions OFF
-- The C compiler identification is MSVC 19.37.32824.0
-- The CXX compiler identification is MSVC 19.37.32824.0
...

Build; I've included output

test\cmake-build-debug $ cmake --build .
MSBuild version 17.7.2+d6990bcfa for .NET Framework
 
  main.cpp
~\.conan2\p\b\abseiebe27282a3fe3\p\include\absl/strings/internal/cord_rep_flat.h(45,39): error C2131: e
xpression did not evaluate to a constant [test\cmake-build-debug\main.vcxproj]
~\.conan2\p\b\abseiebe27282a3fe3\p\include\absl/strings/internal/cord_rep_flat.h(45,39): message : a no
n-constant (sub-)expression was encountered [test\cmake-build-debug\main.vcxproj]
~\.conan2\p\b\abseiebe27282a3fe3\p\include\absl/strings/cord_buffer.h(327,31): error C2131: expression
did not evaluate to a constant [test\cmake-build-debug\main.vcxproj]
~\.conan2\p\b\abseiebe27282a3fe3\p\include\absl/strings/cord_buffer.h(327,31): message : failure was ca
used by non-constant arguments or reference to a non-constant symbol [test\cmake-build-debug\m
ain.vcxproj]
~\.conan2\p\b\abseiebe27282a3fe3\p\include\absl/strings/cord_buffer.h(327,31): message : see usage of '
absl::lts_20230125::CordBuffer::Rep::Short::raw_size' [test\cmake-build-debug\main.vcxproj]