Skip to content

[clang-cl] constexpr fields cannot reference DLL exported symbols unlike with MSVC cl #145400

Open
@andrurogerz

Description

@andrurogerz

This is a minor behavioral difference between clang/clang-cl and MSVC cl when dealing with DLL exported symbols.: when compiling with MSVC, a constexpr variable is allowed to reference a symbol that is DLL-exported. When compiling the same code with clang-cl, an error is produced indicating the variable must be initialized by a constant expression.

Simplest repro:
DLL source:

__declspec(dllexport) int sum(int a, int b) {
  return a + b;
}

Client source:

__declspec(dllimport) void sum();
constexpr auto sumFn = sum;

Compiling this code with clang-cl results in:

error: constexpr variable 'sumFn' must be initialized by a constant expression
  167 |     constexpr auto sumFn = sum;
      |                    ^       ~~~
1 error generated.

Honestly, it seems like Clang is correct to disallow this and MSVC is in the wrong. However, if clang-cl is meant to be a drop-in replacement for cl on Windows then it would need to behave the same.

There is some further discussion in #144386

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang-cl`clang-cl` driver. Don't use for other compiler partsdiverges-from:msvcDoes the clang frontend diverge from msvc on this issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions