Skip to content

Compilation errors with clang on windows: error: constexpr variable 'value' must be initialized by a constant expression #1409

@YuHuanTin

Description

@YuHuanTin
D:\LLVM\bin\clang-cl.exe   -TP  -imsvcD:\Clion\vcpkg\installed\x64-windows\include /DWIN32 /D_WINDOWS /EHsc /Ob0 /Od /RTC1 -std:c++latest -MDd -Zi /clang:-Wall -Wextra -Wpedantic -Wconversion /showIncludes /FoCMakeFiles\rundll.dir\main.cpp.obj /FdCMakeFiles\rundll.dir\ -c -- D:\Clion\Project\rundll\main.cpp
In file included from D:\Clion\Project\rundll\main.cpp:1:
In file included from E:\VS\ToolChain\VC\Tools\MSVC\14.43.34808\include\fstream:10:
In file included from E:\VS\ToolChain\VC\Tools\MSVC\14.43.34808\include\__msvc_filebuf.hpp:12:
In file included from E:\VS\ToolChain\VC\Tools\MSVC\14.43.34808\include\streambuf:10:
In file included from E:\VS\ToolChain\VC\Tools\MSVC\14.43.34808\include\xiosbase:11:
In file included from E:\VS\ToolChain\VC\Tools\MSVC\14.43.34808\include\system_error:10:
In file included from E:\VS\ToolChain\VC\Tools\MSVC\14.43.34808\include\__msvc_system_error_abi.hpp:10:
In file included from E:\VS\ToolChain\VC\Tools\MSVC\14.43.34808\include\cstddef:12:
E:\VS\ToolChain\VC\Tools\MSVC\14.43.34808\include\xtr1common(27,26): error: constexpr variable 'value' must be initialized by a constant expression
   27 |     static constexpr _Ty value = _Val;
      |                          ^       ~~~~
E:\VS\ToolChain\VC\Tools\MSVC\14.43.34808\include\xtr1common(33,16): note: in instantiation of static data member 'std::integral_constant<int (*)(sqlite3_stmt *), &sqlite3_finalize>::value' requested here
   33 |         return value;
      |                ^
E:\VS\ToolChain\VC\Tools\MSVC\14.43.34808\include\memory(3409,13): note: in instantiation of member function 'std::integral_constant<int (*)(sqlite3_stmt *), &sqlite3_finalize>::operator int (*)(sqlite3_stmt *)' requested here
 3409 |             _Mypair._Get_first()(_Mypair._Myval2);
      |             ^
D:\Clion\vcpkg\installed\x64-windows\include\sqlite_orm/sqlite_orm.h(13652,83): note: in instantiation of member function 'std::unique_ptr<sqlite3_stmt, std::integral_constant<int (*)(sqlite3_stmt *), &sqlite3_finalize>>::~unique_ptr' requested here
 13652 |             mapped_iterator(const db_objects_type& dbObjects, statement_finalizer stmt) :
       |                                                                                   ^
In file included from D:\Clion\Project\rundll\main.cpp:1:
In file included from E:\VS\ToolChain\VC\Tools\MSVC\14.43.34808\include\fstream:10:
In file included from E:\VS\ToolChain\VC\Tools\MSVC\14.43.34808\include\__msvc_filebuf.hpp:12:
In file included from E:\VS\ToolChain\VC\Tools\MSVC\14.43.34808\include\streambuf:10:
In file included from E:\VS\ToolChain\VC\Tools\MSVC\14.43.34808\include\xiosbase:11:
In file included from E:\VS\ToolChain\VC\Tools\MSVC\14.43.34808\include\system_error:10:
In file included from E:\VS\ToolChain\VC\Tools\MSVC\14.43.34808\include\__msvc_system_error_abi.hpp:10:
In file included from E:\VS\ToolChain\VC\Tools\MSVC\14.43.34808\include\cstddef:12:
E:\VS\ToolChain\VC\Tools\MSVC\14.43.34808\include\xtr1common(27,26): error: constexpr variable 'value' must be initialized by a constant expression
   27 |     static constexpr _Ty value = _Val;
      |                          ^       ~~~~
E:\VS\ToolChain\VC\Tools\MSVC\14.43.34808\include\xtr1common(33,16): note: in instantiation of static data member 'std::integral_constant<void (*)(void *), &sqlite3_free>::value' requested here
   33 |         return value;
      |                ^
E:\VS\ToolChain\VC\Tools\MSVC\14.43.34808\include\memory(3409,13): note: in instantiation of member function 'std::integral_constant<void (*)(void *), &sqlite3_free>::operator void (*)(void *)' requested here
 3409 |             _Mypair._Get_first()(_Mypair._Myval2);
      |             ^
D:\Clion\vcpkg\installed\x64-windows\include\sqlite_orm/sqlite_orm.h(14189,30): note: in instantiation of member function 'std::unique_ptr<char, std::integral_constant<void (*)(void *), &sqlite3_free>>::~unique_ptr' requested here
 14189 |                 if (char_ptr sql{sqlite3_expanded_sql(this->stmt)}) {
       |                              ^

I found the following, and it seems that clang does not yet support constexpr constraints for a dllimport function.

llvm/llvm-project#125952
llvm/llvm-project#50900

Possible solutions:

#ifdef __clang__
        struct sqlite3_finalizer {
            int operator()(sqlite3_stmt *pStmt) {
                return sqlite3_finalize(pStmt);
            }
        };
        using statement_finalizer = std::unique_ptr<sqlite3_stmt, sqlite3_finalizer>;
#else
    using statement_finalizer =
        std::unique_ptr<sqlite3_stmt, std::integral_constant<decltype(&sqlite3_finalize), sqlite3_finalize>>;
#endif
#ifdef __clang__
                struct sqlite3_freer {
                    void operator()(void* ptr) {
                        sqlite3_free(ptr);
                    }
                };
                using char_ptr = std::unique_ptr<char, sqlite3_freer>;
#else
                using char_ptr = std::unique_ptr<char, std::integral_constant<decltype(&sqlite3_free), sqlite3_free>>;
#endif

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions