public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/52032] New: Function and class attributes should optionally take a bool parameter enabling them
@ 2012-01-28 23:46 joseph.h.garvin at gmail dot com
  2012-01-30 10:06 ` [Bug c++/52032] " rguenth at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: joseph.h.garvin at gmail dot com @ 2012-01-28 23:46 UTC (permalink / raw)
  To: gcc-bugs

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: unassigned@gcc.gnu.org
        ReportedBy: joseph.h.garvin@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.


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c++/52032] Function and class attributes should optionally take a bool parameter enabling them
  2012-01-28 23:46 [Bug c++/52032] New: Function and class attributes should optionally take a bool parameter enabling them joseph.h.garvin at gmail dot com
@ 2012-01-30 10:06 ` rguenth at gcc dot gnu.org
  2012-01-30 13:43 ` joseph.h.garvin at gmail dot com
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: rguenth at gcc dot gnu.org @ 2012-01-30 10:06 UTC (permalink / raw)
  To: gcc-bugs

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

Richard Guenther <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |WONTFIX

--- Comment #1 from Richard Guenther <rguenth at gcc dot gnu.org> 2012-01-30 09:37:01 UTC ---
You can use explicit template specialization to select between two variants.


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c++/52032] Function and class attributes should optionally take a bool parameter enabling them
  2012-01-28 23:46 [Bug c++/52032] New: Function and class attributes should optionally take a bool parameter enabling them joseph.h.garvin at gmail dot com
  2012-01-30 10:06 ` [Bug c++/52032] " rguenth at gcc dot gnu.org
@ 2012-01-30 13:43 ` joseph.h.garvin at gmail dot com
  2012-01-30 13:47 ` rguenth at gcc dot gnu.org
  2012-01-30 21:39 ` joseph.h.garvin at gmail dot com
  3 siblings, 0 replies; 5+ messages in thread
From: joseph.h.garvin at gmail dot com @ 2012-01-30 13:43 UTC (permalink / raw)
  To: gcc-bugs

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

Joseph Garvin <joseph.h.garvin at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |UNCONFIRMED
         Resolution|WONTFIX                     |

--- Comment #2 from Joseph Garvin <joseph.h.garvin at gmail dot com> 2012-01-30 13:34:55 UTC ---
You can use template specialization to choose between two variants, but that
only solves half the problem (choosing attributes based on template
metaprogramming). The verbosity is still huge. For variable attributes it may
not be as big a deal, but for class/struct attributes changing them means
redefining the entire class/struct including all of its methods. Being able to
pass true/false to change the packed'ness of a struct would be a lot nicer than
copy and pasting a class definition, changing the name, adjusting the
specialization of each method, and then trying to remember to keep two versions
of every method in sync (or dealing with the cognitive overhead of having both
versions dispatch to another layer so that they can share their code).


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c++/52032] Function and class attributes should optionally take a bool parameter enabling them
  2012-01-28 23:46 [Bug c++/52032] New: Function and class attributes should optionally take a bool parameter enabling them joseph.h.garvin at gmail dot com
  2012-01-30 10:06 ` [Bug c++/52032] " rguenth at gcc dot gnu.org
  2012-01-30 13:43 ` joseph.h.garvin at gmail dot com
@ 2012-01-30 13:47 ` rguenth at gcc dot gnu.org
  2012-01-30 21:39 ` joseph.h.garvin at gmail dot com
  3 siblings, 0 replies; 5+ messages in thread
From: rguenth at gcc dot gnu.org @ 2012-01-30 13:47 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Richard Guenther <rguenth at gcc dot gnu.org> 2012-01-30 13:38:07 UTC ---
But that's not how existing GCC attributes work.


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c++/52032] Function and class attributes should optionally take a bool parameter enabling them
  2012-01-28 23:46 [Bug c++/52032] New: Function and class attributes should optionally take a bool parameter enabling them joseph.h.garvin at gmail dot com
                   ` (2 preceding siblings ...)
  2012-01-30 13:47 ` rguenth at gcc dot gnu.org
@ 2012-01-30 21:39 ` joseph.h.garvin at gmail dot com
  3 siblings, 0 replies; 5+ messages in thread
From: joseph.h.garvin at gmail dot com @ 2012-01-30 21:39 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Joseph Garvin <joseph.h.garvin at gmail dot com> 2012-01-30 21:13:37 UTC ---
That's why I put the severity as enhancement :)

I thought of a better example.

template<bool force_inline>
void __attribute__((always_inline(force_inline))) foo()
{
    // ...
}

Now you can choose whether to force inlining of foo on a per call site basis,
which AFAICT there is no way to do in GCC currently. I don't think template
specialization can accomplish the same -- the best you could do would be
defining a function with a copy pasta of the same code but a different name.
You couldn't have both versions call a common function because the force_inline
parameter would need to apply to it as well.


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2012-01-30 21:13 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-01-28 23:46 [Bug c++/52032] New: Function and class attributes should optionally take a bool parameter enabling them joseph.h.garvin at gmail dot com
2012-01-30 10:06 ` [Bug c++/52032] " rguenth at gcc dot gnu.org
2012-01-30 13:43 ` joseph.h.garvin at gmail dot com
2012-01-30 13:47 ` rguenth at gcc dot gnu.org
2012-01-30 21:39 ` joseph.h.garvin at gmail dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).