Preventing L-value instantiation in C++14

Viewed 565

Methods in C++14 can tell whether they're called on an L-value or an R-value:

struct A{
  A() { puts("Ctor"); }
  void m() const & { puts("L-value"); }
  void m() const && { puts("R-value"); }
};

int main(){
  A a; //Ctor
  a.m() //L-value
  A().m(); //Ctor; R-value
}

Can a ctor tell which type it's constructing? Can I completely disable the construction of L-values from my class?

I have a proxy class (several, actually), which should always convert to something else. Using it without converting is an error. I can detect that error at runtime, e.g., by adding a bool used_ = 0; member #ifndef NDEBUG; and setting it in my user-specified cast, and then doing assert(used_) in the proxy class's Dtor, however it would be much nicer if I could get to compiler to prevent instatiation of L-value instances of that proxy in the first place:

 auto x = Proxy().method1().method2(); // no
 Proxy p; // no
 Target x = Proxy(); //yes
 Target x = Proxy().method1().method2(); //yes

Can I do something like that with C++14?

2 Answers
Related