[cfe-users] Anyway to prevent this code from compiling?

Brian Cole via cfe-users cfe-users at lists.llvm.org
Mon Feb 29 09:02:23 PST 2016

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:

OELock lock(mutex);

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:

#include <mutex>

using namespace std;

struct OEMutex
  void Acquire() {}
  void Release() {}

static OEMutex _mutex;

class OELock
  OEMutex &_mutex;
  OELock(const OELock&);
  OELock& operator=(const OELock&);

  OELock(OEMutex &mutex) : _mutex(mutex) { _mutex.Acquire(); }
  ~OELock() { _mutex.Release(); }

int main()
  OELock lock(mutex);

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?).


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-users/attachments/20160229/63019790/attachment.html>

More information about the cfe-users mailing list