[cfe-users] inconsistent compilation error inside constexpr if

Manu agarwal via cfe-users cfe-users at lists.llvm.org
Tue Aug 25 23:22:14 PDT 2020


Thanks Richard and David for your replies. That was helpful.

Regards,
Manu

________________________________
From: Richard Smith <richard at metafoo.co.uk>
Sent: Sunday, August 23, 2020 11:56 PM
To: Manu agarwal <Manu.Agarwal at live.in>
Cc: cfe-users at lists.llvm.org <cfe-users at lists.llvm.org>
Subject: Re: [cfe-users] inconsistent compilation error inside constexpr if

On Fri, 21 Aug 2020 at 13:29, Manu agarwal via cfe-users <cfe-users at lists.llvm.org<mailto:cfe-users at lists.llvm.org>> wrote:
Hello,

In the below code the compiler throws "undeclared identifier" when the commented line is uncommented. Whereas the line just before compiles fine.

Regards,
Manu

typedef bool (* DummyFunc)   ();


bool ExecDummy (DummyFunc fptr) {

    if (fptr)
        return fptr ();

    return false;
}

constexpr unsigned int IsMyAppClient = 0;

constexpr bool CheckForTypeClient (unsigned int pAppType)
{
    return ((pAppType & IsMyAppClient) != 0);
}


class  MyAppTemplate
{
public:
    template <unsigned int T>
    static    bool    MyAppInit ();
};

template <unsigned int T>
bool
MyAppTemplate::MyAppInit ()
{
    if constexpr (CheckForTypeClient(T)) {

        return ClientMain ();                    // no error
        //return ExecDummy(ClientMain);            // error: use of undeclared identifier 'ClientMain'
    }

This code is invalid, as David Blaikie explains. However, in MSVC-compatible mode (under -fms-compatibility, which is enabled by default for some Windows-targeting modes), Clang attempts to to accept certain invalid code that MSVC has historically accepted, including this case, where we allow the call to ClientMain() to find declarations of a ClientMain function that appear after the template definition. That recovery from invalid code is only done for certain syntactic patterns, such as unqualified function calls -- so it applies to ClientMain() but not to (ClientMain).


    return false;
}

int __cdecl
main (int pArgc, char* pArgv[])
{
    constexpr int TVal = 3;

    MyAppTemplate::MyAppInit<TVal> ();

    return 0;
}


_______________________________________________
cfe-users mailing list
cfe-users at lists.llvm.org<mailto:cfe-users at lists.llvm.org>
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-users
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-users/attachments/20200826/09ae277d/attachment.html>


More information about the cfe-users mailing list