<div dir="ltr"><div>Yes, get ride of using namespace std.</div><div><br></div>The problem is the parsing. Since mutex is a type, OELock lock(mutex) is function declaration, i.e., returns an OELock and takes a mutex as a parameter. <div><br></div><div>Could you use factory function instead?</div><div><br></div><div><div>lass OELock</div><div>{</div><div> OEMutex &_mutex;</div><div> OELock();</div><div> //OELock(const OELock&); // is this a problem for you???</div><div> OELock& operator=(const OELock&);</div><div> OELock(OEMutex &_mutex) : _mutex(_mutex) { _mutex.Acquire(); }</div><div>public:</div><div> static OELock makeLock(OEMutex &mutex) { return OELock(mutex);}</div><div><br></div><div> ~OELock() { _mutex.Release(); }</div><div>};</div><div><br></div><div>OELock lock(mutex);</div><div><br></div><div>int main()</div><div>{</div><div> OELock lock = OELock::makeLock(mutex);</div><div>}</div></div><div><div><br></div><div><br></div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Feb 29, 2016 at 1:28 PM, Brian Cole <span dir="ltr"><<a href="mailto:coleb@eyesopen.com" target="_blank">coleb@eyesopen.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div style="word-wrap:break-word;color:rgb(0,0,0);font-size:14px;font-family:Calibri,sans-serif">
<div>
<div>Anything that can be added to the OELock class to make it uncompilable in that context? Getting users to change how they initialize a scoped lock won’t be easy. </div>
<div><br>
</div>
<div>A “just as easy” solution is to remove the ‘using namespace std’. I guess this is why Google banned ‘using namespace foo;’ in their style guide: <a href="https://google.github.io/styleguide/cppguide.html#Namespaces" target="_blank">https://google.github.io/styleguide/cppguide.html#Namespaces</a></div>
<div>
<div></div>
</div>
</div>
<div><br>
</div>
<span>
<div style="font-family:Calibri;font-size:12pt;text-align:left;color:black;BORDER-BOTTOM:medium none;BORDER-LEFT:medium none;PADDING-BOTTOM:0in;PADDING-LEFT:0in;PADDING-RIGHT:0in;BORDER-TOP:#b5c4df 1pt solid;BORDER-RIGHT:medium none;PADDING-TOP:3pt">
<span style="font-weight:bold">From: </span>don hinton <<a href="mailto:hintonda@gmail.com" target="_blank">hintonda@gmail.com</a>><br>
<span style="font-weight:bold">Date: </span>Monday, February 29, 2016 at 11:22 AM<div><div class="h5"><br>
<span style="font-weight:bold">To: </span>Brian L Cole <<a href="mailto:coleb@eyesopen.com" target="_blank">coleb@eyesopen.com</a>><br>
<span style="font-weight:bold">Cc: </span>"<a href="mailto:cfe-users@lists.llvm.org" target="_blank">cfe-users@lists.llvm.org</a>" <<a href="mailto:cfe-users@lists.llvm.org" target="_blank">cfe-users@lists.llvm.org</a>><br>
<span style="font-weight:bold">Subject: </span>Re: [cfe-users] Anyway to prevent this code from compiling?<br>
</div></div></div><div><div class="h5">
<div><br>
</div>
<div>
<div>
<div dir="ltr">you could try adding an extra set of parentheses, but you won't get as good an error message.
<div><br>
</div>
<div><span style="font-family:Calibri,sans-serif;font-size:12.8px">OELock lock((mutex));</span><br>
</div>
</div>
<div class="gmail_extra"><br>
<div class="gmail_quote">On Mon, Feb 29, 2016 at 1:15 PM, Brian Cole <span dir="ltr">
<<a href="mailto:coleb@eyesopen.com" target="_blank">coleb@eyesopen.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div style="word-wrap:break-word;color:rgb(0,0,0);font-size:14px;font-family:Calibri,sans-serif">
<div>
<div>Was hoping for something that would be C++03 compatible as well since we still have C++03 compilers to target as well.</div>
<div>
<div></div>
</div>
</div>
<div><br>
</div>
<span>
<div style="font-family:Calibri;font-size:12pt;text-align:left;color:black;BORDER-BOTTOM:medium none;BORDER-LEFT:medium none;PADDING-BOTTOM:0in;PADDING-LEFT:0in;PADDING-RIGHT:0in;BORDER-TOP:#b5c4df 1pt solid;BORDER-RIGHT:medium none;PADDING-TOP:3pt">
<span style="font-weight:bold">From: </span>don hinton <<a href="mailto:hintonda@gmail.com" target="_blank">hintonda@gmail.com</a>><br>
<span style="font-weight:bold">Date: </span>Monday, February 29, 2016 at 10:38 AM<br>
<span style="font-weight:bold">To: </span>Brian L Cole <<a href="mailto:coleb@eyesopen.com" target="_blank">coleb@eyesopen.com</a>><br>
<span style="font-weight:bold">Cc: </span>"<a href="mailto:cfe-users@lists.llvm.org" target="_blank">cfe-users@lists.llvm.org</a>" <<a href="mailto:cfe-users@lists.llvm.org" target="_blank">cfe-users@lists.llvm.org</a>><br>
<span style="font-weight:bold">Subject: </span>Re: [cfe-users] Anyway to prevent this code from compiling?<br>
</div>
<div>
<div>
<div><br>
</div>
<div>
<div>
<div dir="ltr">
<div>Try using initialization list syntax. That way the parser won't think you are declaring a function.</div>
<div><br>
</div>
<div><span style="font-family:Calibri,sans-serif;font-size:12.8px">OELock lock{mutex};</span><br>
</div>
<div><span style="font-family:Calibri,sans-serif;font-size:12.8px"><br>
</span></div>
<div><span style="font-family:Calibri,sans-serif;font-size:12.8px"><br>
</span></div>
</div>
<div class="gmail_extra"><br>
<div class="gmail_quote">On Mon, Feb 29, 2016 at 12:02 PM, Brian Cole via cfe-users
<span dir="ltr"><<a href="mailto:cfe-users@lists.llvm.org" target="_blank">cfe-users@lists.llvm.org</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div style="word-wrap:break-word">
<div>
<div>
<div style="color:rgb(0,0,0);font-family:Calibri,sans-serif;font-size:14px">Since switching over to clang C++11 on OS X, we had this weird C++ oddity surface while writing some new code. The problem is that ‘mutex’ is no longer a variable, it is a class type
that can be interpreted as a function argument. That is, the following line of code can be interpreted as a function declaration now:</div>
<div style="color:rgb(0,0,0);font-family:Calibri,sans-serif;font-size:14px"><br>
</div>
<div style="color:rgb(0,0,0);font-family:Calibri,sans-serif;font-size:14px">OELock lock(mutex);</div>
<div style="color:rgb(0,0,0);font-family:Calibri,sans-serif;font-size:14px"><br>
</div>
<div style="color:rgb(0,0,0);font-family:Calibri,sans-serif;font-size:14px">Instead of a scoped lock acquisition as has been convention for some time now. The full code to recreate the subtle bug is as follows: </div>
<div style="color:rgb(0,0,0);font-family:Calibri,sans-serif;font-size:14px"><br>
</div>
<div>
<div><font face="Calibri,sans-serif">#include <mutex></font></div>
<div><font face="Calibri,sans-serif"><br>
</font></div>
<div><font face="Calibri,sans-serif">using namespace std;</font></div>
<div><font face="Calibri,sans-serif"><br>
</font></div>
<div><font face="Calibri,sans-serif">struct OEMutex</font></div>
<div><font face="Calibri,sans-serif">{</font></div>
<div><font face="Calibri,sans-serif"> void Acquire() {}</font></div>
<div><font face="Calibri,sans-serif"> void Release() {}</font></div>
<div><font face="Calibri,sans-serif">};</font></div>
<div><font face="Calibri,sans-serif"><br>
</font></div>
<div><font face="Calibri,sans-serif">static OEMutex _mutex;</font></div>
<div><font face="Calibri,sans-serif"><br>
</font></div>
<div><font face="Calibri,sans-serif">class OELock</font></div>
<div><font face="Calibri,sans-serif">{</font></div>
<div><font face="Calibri,sans-serif"> OEMutex &_mutex;</font></div>
<div><font face="Calibri,sans-serif"> OELock();</font></div>
<div><font face="Calibri,sans-serif"> OELock(const OELock&);</font></div>
<div><font face="Calibri,sans-serif"> OELock& operator=(const OELock&);</font></div>
<div><font face="Calibri,sans-serif"><br>
</font></div>
<div><font face="Calibri,sans-serif">public:</font></div>
<div><font face="Calibri,sans-serif"> OELock(OEMutex &mutex) : _mutex(mutex) { _mutex.Acquire(); }</font></div>
<div><font face="Calibri,sans-serif"> ~OELock() { _mutex.Release(); }</font></div>
<div><font face="Calibri,sans-serif">};</font></div>
<div><font face="Calibri,sans-serif"><br>
</font></div>
<div><font face="Calibri,sans-serif">int main()</font></div>
<div><font face="Calibri,sans-serif">{</font></div>
<div><font face="Calibri,sans-serif"> OELock lock(mutex);</font></div>
<div><font face="Calibri,sans-serif">}</font></div>
</div>
<div><font face="Calibri,sans-serif"><br>
</font></div>
<div>Ideally, we would like the compilation to fail and tell the user the ‘mutex’ variable can not be found. Any clever C++ trick to do that? We’ve tried declaring the move constructors of OELock to be private, but it still compiles (maybe that’s SFINAE?). </div>
<div><br>
</div>
<div>Thanks,</div>
<div>Brian</div>
<div style="color:rgb(0,0,0);font-family:Calibri,sans-serif;font-size:14px"><br>
</div>
<div style="color:rgb(0,0,0);font-family:Calibri,sans-serif;font-size:14px">
<div></div>
</div>
</div>
</div>
</div>
<br>
_______________________________________________<br>
cfe-users mailing list<br>
<a href="mailto:cfe-users@lists.llvm.org" target="_blank">cfe-users@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-users" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-users</a><br>
<br>
</blockquote>
</div>
<br>
</div>
</div>
</div>
</div>
</div>
</span></div>
</blockquote>
</div>
<br>
</div>
</div>
</div>
</div></div></span>
</div>
</blockquote></div><br></div>