http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52032

             Bug #: 52032
           Summary: Function and class attributes should optionally take a
                    bool parameter enabling them
    Classification: Unclassified
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: enhancement
          Priority: P3
         Component: c++
        AssignedTo: unassig...@gcc.gnu.org
        ReportedBy: joseph.h.gar...@gmail.com


Summary: Attributes like packed, always_inline, etc. should support taking a
bool to enable them, similar to how 'noexcept' works in C++11.


Details:

In C++11, one can add the 'noexcept' specifier on to a function to indicate
that it doesn't throw exceptions, e.g.:

void foo() noexcept
{
    // ...
}

However, to assist with template metaprogramming, noexcept can take compile
time constant bool to switch whether it's in effect:

struct MyCallback
{
};

template<class CallbackT>
void foo() noexcept(!CallbackT::ThrowsExceptions)
{
    CallbackT::action();
}

It would reduce redundant code if GCC let you do this in general for its
attributes. For example, currently if you want both a packed and unpacked
version of a struct, you are forced to either define the struct twice or
generate both versions with a macro. Ideally you could do this:

template<bool should_pack=false>
struct __attribute__((__packed__(should_pack))) MyStruct
{
    // ... members
}

typedef MyStruct<true> PackedMyStruct;

This functionality could be useful for pretty much all of GCC's attributes.

Reply via email to