C++ Logo

std-proposals

Advanced search

[std-proposals] std::terminate_compilation

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Fri, 19 May 2023 11:17:18 +0100
I wrote the following template function today. The type, 'SenderId', is an enum.

    template<SenderId id>
    auto &GetSender(void)
    {
             if constexpr ( id == eArp ) return
dynamic_cast<Sender_Arp& >(GetSender_BaseClass(eArp ));
        else if constexpr ( id == eNetBios ) return
dynamic_cast<Sender_NetBios& >(GetSender_BaseClass(eNetBios ));
        else if constexpr ( id == eProbe ) return
dynamic_cast<Sender_Probe& >(GetSender_BaseClass(eProbe ));
        else if constexpr ( id == ePortScan ) return
dynamic_cast<Sender_PortScan& >(GetSender_BaseClass(ePortScan ));
        else if constexpr ( id == eFileShare ) return
dynamic_cast<Sender_FileShare&>(GetSender_BaseClass(eFileShare));
        else if constexpr ( id == eSecuriCam ) return
dynamic_cast<Sender_SecuriCam&>(GetSender_BaseClass(eSecuriCam));
        else
        {
            static int const i = -1;
            return i;
        }
    }


Before I settled on returning an int&, I had tried:

    static_assert( false == true, "Terminate compilation" );

and also:

    typedef char dummy[-5];

but neither of them would compile. Version 12 of g++ is trying to
compile the 'else leg' of the the 'if constexpr' even when it never
branches there.

What should be the canonical way of expressing what I want to express
here? Should we have something like:

if constexpr ( .... ) { . . . }
else if constexpr ( ... ) { . . . }
else
{
    std::terminate_compilation();
}

Received on 2023-05-19 10:17:30