1

Trying to disable elide constructors for one function, the following function gives me the expected output under GCC, but not under clang. Under GCC, the output includes copy cstr called as expected, but not under clang, which gives the warning: unknown attribute 'optimize' ignored [-Wattributes] What could be happening? Should I be using anther keyword in place of 'optimize'?

#include <iostream>

class xyz {
 public:
  xyz() { std::cout << "cstr called\n"; }
  xyz(const xyz& A) { std::cout << "copy cstr called\n"; }
  xyz f() __attribute__((optimize("no-elide-constructors"))) {
    std::cout << "f called\n";
    xyz x;

    return x;
  }
};

int main() {
  xyz a;
  xyz b = a.f();
}
pulsejet
  • 1,101
  • 13
  • 27
  • 3
    `__attribute__` are an extension in the GCC and Clang compilers. They do not share the same extensions, and therefore may have different `__attribute__` types and options. – Some programmer dude Mar 07 '17 at 12:24
  • @Someprogrammerdude, but AFAIK, -fno-elide-constructors works on both Clang and GCC, hence I guess both should respect this attribute. – pulsejet Mar 07 '17 at 12:26
  • 3
    @RadialApps Just because a two compilers support the same command line switch is the same, doesn't mean they support the same attributes. – fredrik Mar 07 '17 at 12:32
  • @Someprogrammerdude don't answer in comments ;) – Caleth Mar 07 '17 at 14:01
  • @Caleth Didn't mean for it to be an answer. :) – Some programmer dude Mar 07 '17 at 15:00
  • AFAIK impossible for clang, there is only an optnone attribute (check the proposal http://clang-developers.42468.n3.nabble.com/RFC-add-Function-Attribute-to-disable-optimization-td4032641.html). A per-function disable-specific-attribute-functionality is not supported. – overseas Mar 07 '17 at 22:28
  • @overseas, I tried optnone, but it doesn't seem to disable elide constructors either. Possibly because of the reasons [here](http://stackoverflow.com/questions/21070513/is-fno-elide-constructors-contained-in-o0-or-any-other-optimization-level?rq=1) – pulsejet Mar 08 '17 at 08:21

0 Answers0