FWIW even though applying WARN_UNUSED_RESULT to a type is a C++17
extension, it works fine in C++11 on recent versions of Clang and gcc:

todd@todd-ThinkPad-T540p:/tmp$ cat test.cc
struct __attribute__((warn_unused_result))
Foo {
};


Foo bar() {
  return Foo();
}

int main() {
  bar();
  return 0;
}
todd@todd-ThinkPad-T540p:/tmp$ g++ --version ; g++ -o test test.cc
g++ (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

test.cc:2:1: warning: ‘warn_unused_result’ attribute only applies to
function types [-Wattributes]
 Foo {
 ^
todd@todd-ThinkPad-T540p:/tmp$ clang++ --version; clang++ -o test test.cc
clang version 3.9.0 (tags/RELEASE_390/final)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /home/todd/git/kudu/thirdparty/clang-toolchain/bin
test.cc:11:3: warning: ignoring return value of function declared with
'warn_unused_result' attribute [-Wunused-result]
  bar();
  ^~~
1 warning generated.


On Fri, Jan 6, 2017 at 3:08 PM, Thomas Tauber-Marshall <
[email protected]> wrote:

> I'd vote for option 1, given that, as you say, this is closely related to
> the return type, which putting the macro with the type makes clear.
>
> Also looking at buffer-pool.h in the review, it doesn't look overly noisy
> to me.
>
> On Fri, Jan 6, 2017 at 2:59 PM Tim Armstrong <[email protected]>
> wrote:
>
> > Hi All,
> >   I wanted to poll the Impala community for opinions about style for
> > declaring functions where the caller is expected to do something with the
> > return value.
> >
> > Ideally we'd be able to declare Status with an attribute that made this
> > take effect globally, but unfortunately that's not available until C++17.
> >
> > So we need to annotate each Status-returning function. The two
> alternatives
> > we discussed on this CR (https://gerrit.cloudera.org/#/c/4878/) were:
> >
> > #1 - a special macro wrapping Status
> >
> > MUST_USE(Status) DoSomethingThatCanFail(int64_t foo, Bar* bar);
> >
> > Pros:
> > * Closely connected to the return type that it affects
> > * It's easier to search/replace Status with MUST_USE(Status)
> >
> > Cons:
> > * Could get visually noisy if we use it everywhere
> >
> > #2 - a macro that gets appended to the declaration:
> >
> > Status DoSomethingThatCanFail(int64_t foo, Bar* bar) WARN_UNUSED_RESULT;
> >
> > Pros:
> > * Macro is slightly
> > * Less visually noisy since it's at the end of the declaration
> >
> > What do people think?
> >
>



-- 
Todd Lipcon
Software Engineer, Cloudera

Reply via email to