Skip to content

[clang] Incorrect constexpr member function definition order dependency #156255

@pdimov

Description

@pdimov

The following code

class X
{
public:

    constexpr int f( int x ) const
    {
        return g( x );
    }

private:

    template<class T>
    constexpr T g( T x ) const
    {
        return x;
    }
};

constexpr int x = X().f( 1 );

is rejected by Clang trunk (and all previous versions AFAICS) with

<source>:7:16: note: undefined function 'g<int>' cannot be used in a constant expression
    7 |         return g( x );
      |                ^

and in order to fix that, the definition of g needs to be moved above that of f.

But I don't think this order dependency is required by the standard, and other compilers accept the code.

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:frontendLanguage frontend issues, e.g. anything involving "Sema"constexprAnything related to constant evaluationdiverges-from:gccDoes the clang frontend diverge from gcc on this issuediverges-from:msvcDoes the clang frontend diverge from msvc on this issueduplicateResolved as duplicate

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions