Skip to content

Clang++ compiler error about static constexpr member function #55315

Closed
@akr-akari

Description

The code is as follows.

struct Test
{
    static constexpr void foo1() noexcept
    {
        fooImpl(2022);
    }

    static constexpr void foo2() noexcept
    {
        fooImpl(3.14);
    }

    template<class T>
    static constexpr void fooImpl(T value) noexcept
    {
    }
};

int main()
{
    Test t;
    t.foo1();
    t.foo2();
}

In gcc, it compiles successfully. But in clang, the compilation fails and the output is as follows.

> clang++ main.cc -omain.exe
main.cc:19:27: warning: inline function 'Test::fooImpl<int>' is not defined
      [-Wundefined-inline]
    static constexpr void fooImpl(T value) noexcept
                          ^
main.cc:10:9: note: used here
        fooImpl(2022);
        ^
main.cc:19:27: warning: inline function 'Test::fooImpl<double>' is not defined
      [-Wundefined-inline]
    static constexpr void fooImpl(T value) noexcept
                          ^
main.cc:15:9: note: used here
        fooImpl(3.14);
        ^
2 warnings generated.
C:/Users/XIANG/winlibs/mingw64/bin/ld: C:/Users/XIANG/AppData/Local/Temp/main-640e60.o:main.cc:(.text$_ZN4Test4foo1Ev[_ZN4Test4foo1Ev]+0xa): undefined reference to `void Test::fooImpl<int>(int)'
C:/Users/XIANG/winlibs/mingw64/bin/ld: C:/Users/XIANG/AppData/Local/Temp/main-640e60.o:main.cc:(.text$_ZN4Test4foo2Ev[_ZN4Test4foo2Ev]+0xd): undefined reference to `void Test::fooImpl<double>(double)'
clang++: error: linker command failed with exit code 1 (use -v to see invocation)

But I put fooImpl() at the top and it compiles successfully.

The code is as follows.

struct Test
{
    template<class T>
    static constexpr void fooImpl(T value) noexcept
    {
    }

    static constexpr void foo1() noexcept
    {
        fooImpl(2022);
    }

    static constexpr void foo2() noexcept
    {
        fooImpl(3.14);
    }
};

int main()
{
    Test t;
    t.foo1();
    t.foo2();
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Labels

clang:frontendLanguage frontend issues, e.g. anything involving "Sema"

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions