Constantin Constantin - 15 days ago 5
C++ Question

Move Constructor Complaining about "non-class type"

All, I'm playing around with understanding r-value, l-value and what scott meyers calls "universal references". I'm having some trouble setting up my example, shown below. Could anyone offer some guidance?

#include <cstdio>

namespace who_needs_namespaces {

class SomeSillyClass {
public:
SomeSillyClass() {
printf("Default constructed.\n");
}
SomeSillyClass(const SomeSillyClass& rhs) {
printf("L-Value constructed.\n");
}
SomeSillyClass(SomeSillyClass&& rhs) {
printf("R-Value Constructed.\n");
}

void doStuff() const {
printf("doing...\n");
}
};

template<typename T>
class HolderByValue {
public:
HolderByValue() = delete;
HolderByValue(T toHold) : m_mine(toHold)
{ };

private:
T m_mine;
};

template<typename T>
class HolderByRef {
public:
HolderByRef() = delete;
HolderByRef(T&& toHold) : m_mine(toHold)
{ };

private:
T m_mine;
};

}

using namespace who_needs_namespaces;

int main() {
//Check our silly class.
SomeSillyClass defaultCtr;
defaultCtr.doStuff();
SomeSillyClass copyCtr(defaultCtr);
copyCtr.doStuff();
SomeSillyClass moveCtr(SomeSillyClass);
moveCtr.doStuff();

//Check our HolderByValue
//HolderByValue<SomeSillyClass> holder(defaultCtr);
HolderByValue<SomeSillyClass &> holder(defaultCtr);

}





main.cpp: In function 'int main()':
main.cpp:59:13: error: request for member 'doStuff' in 'moveCtr', which is of non-class type 'who_needs_namespaces::SomeSillyClass(who_needs_namespaces::SomeSillyClass)'

Answer

I think you've fallen victim to what Scott Meyers calls C++'s Most Vexing Parse. https://en.wikipedia.org/wiki/Most_vexing_parse