Skip to content

On Windows, can't create constexpr assignment for function pointer of function marked with dllimport #125952

Open
@cgagnon-mw

Description

@cgagnon-mw

I couldn't find this issue reported here, but if it's already been handled, feel free to archive this.

This code compiles on Mac and Linux (using the equivalent import/export annotations), but not on Windows:

class __declspec(dllimport) MyClass {
    public:
     int* foo(int) const;
};
static constexpr auto(MyClass::*bar)(int) const = &MyClass::foo;

Here's the error output:

<source>:6:33: error: constexpr variable 'bar' must be initialized by a constant expression
static constexpr auto(MyClass::*bar)(int) const = &MyClass::foo;
                                ^                 ~~~~~~~~~~~~~

The program compiles on Windows if you replace dllimport with dllexport.
It also compiles if you simply remove constexpr. Using constinit gives essentially the same error.

Here's a similar issue report for MSVC (not authored by me).
https://developercommunity.visualstudio.com/t/cannot-constinitconstexpr-initialize-a/10456333

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions