<html>
<head>
<style><!--
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
font-size: 12pt;
font-family:Calibri
}
--></style></head>
<body class='hmmessage'><div dir='ltr'>When using clang (3.6.0, on Ubuntu 15.04) to build a C++ class with __declspec(property), we observed a strange behavior, please see sample code below.<BR> <BR><font face="Courier New,sans-serif">00  #include <stdio.h></font><BR><font face="Courier New,sans-serif">01  class Test1 {</font><BR><font face="Courier New,sans-serif">02  private:<br>03      int x_;<br>04  public:<br>05      Test1(int x) : x_(x) {}<br>06      __declspec(property(get=get_x)) int X;<br>07      int get_x() const { return x_; }</font><BR><font face="Courier New,sans-serif">08      static Test1* GetTest1() { printf("create, "); return new Test1(10); }</font><BR><font face="Courier New,sans-serif">09  };</font><BR><font face="Courier New,sans-serif">10  int main(int argc, char** argv) {<br>11      int x = Test1::GetTest1()->X;<br>12      printf("x=%d\n",x);<br>13  }</font><BR> <BR>The execution output is like: <BR>create, create, x=10<BR> <BR>Basically, the compiler builds the code line 11 into<BR><font color="#555555" face="Consolas" size="2"><font color="#555555" face="Consolas" size="2"><font color="#555555" face="Consolas" size="2"><font face="Courier New,sans-serif">0x000000000040077f  call 0x4007c0 <Test1::GetTest1()> </font><BR><font face="Courier New,sans-serif">
0x0000000000400784  mov QWORD PTR [rbp-0x20],rax </font><BR><font face="Courier New,sans-serif">
0x0000000000400788  call 0x4007c0 <Test1::GetTest1()> </font><BR><font face="Courier New,sans-serif">
0x000000000040078d  mov rdi,rax </font><BR><font face="Courier New,sans-serif">
0x0000000000400790  call 0x400830 <Test1::get_x() const> </font><BR><font face="Courier New,sans-serif">
0x0000000000400795  movabs rdi,0x4008e4 </font><BR><font face="Courier New,sans-serif">
0x000000000040079f  mov DWORD PTR [rbp-0x14],eax </font><BR></font> <BR></font></font>As we notice, Test1::GetTest1() is called twice. The result of the 1st call is stored into a compiler-allocated stack variable, and seems never used later; The result of the 2nd call is passed as "this" pointer to Test1::get_x(). Is this an expected behavior? Since our real GetTest1() is not idempotent, being called twice is causing incorrect program behavior. Could any folk working on __declspec(property) feature of clang take a look? Thanks a lot in advance. <BR> <BR>                                       </div></body>
</html>