Note that there are some explanatory texts on larger screens.

plurals
  1. POG++: Can __attribute__((__may_alias__)) be used for pointer to class instance rather than to class definition itself?
    primarykey
    data
    text
    <p>I'm looking to the answer to the following question: is <code>may_alias</code> suitable as attribute for pointer to an object of some class <code>Foo</code>? Or must it be used at class level only? </p> <p>Consider the following code(it is based on a real-world example which is more complex): </p> <pre><code>#include &lt;iostream&gt; using namespace std; #define alias_hack __attribute__((__may_alias__)) template &lt;typename T&gt; class Foo { private: /*alias_hack*/ char Data[sizeof (T)]; public: /*alias_hack*/ T&amp; GetT() { return *((/*alias_hack*/ T*)Data); } }; struct Bar { int Baz; Bar(int baz) : Baz(baz) {} } /*alias_hack*/; // &lt;- uncommeting this line apparently solves the problem, but does so on class-level(rather than pointer-level) // uncommenting previous alias_hack's doesn't help int main() { Foo&lt;Bar&gt; foo; foo.GetT().Baz = 42; cout &lt;&lt; foo.GetT().Baz &lt;&lt; endl; } </code></pre> <p>Is there any way to tell gcc that single pointer <code>may_alias</code> some another?</p> <p>BTW, please note that gcc detection mechanism of such problem is imperfect, so it is very easy to just make this warning go away without actually solving the problem.</p> <p>Consider the following snippet of code:</p> <pre><code>#include &lt;iostream&gt; using namespace std; int main() { long i = 42; long* iptr = &amp;i; //(*(short*)&amp;i) = 3; // with warning //(*(short*)iptr) = 3; // without warning cout &lt;&lt; i &lt;&lt; endl; } </code></pre> <p>Uncomment one of the lines to see the difference in compiler output.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload