C ++ 14 methods can determine if they are caused by 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;
Can ctor indicate what type it is building? Can I completely disable building L-values ββfrom my class?
I have a proxy class (several, actually) that should always convert to something else. Using it without conversion is a mistake. I can detect this error at runtime, for example by adding the member bool used_ = 0; #ifndef NDEBUG; and setting it to your user composition and then running assert(used_) in the Dtor proxy class, however it would be much better if I could get a compiler to prevent the initialization of L-value instances of this proxy server in the first place:
auto x = Proxy().method1().method2(); // no Proxy p; // no Target x = Proxy(); //yes Target x = Proxy().method1().method2(); //yes
Is it possible to do something similar with C ++ 14?
c ++ c ++ 14
PSkocik
source share