I tried to make a static statement in the template parameter to check / force use Typefor copy. However, the static statement fails. I do not understand why, and I can not find any documentation why it will not be subjected to static evaluation.
The instantiated class is constructive, but it uses what I think is called a curiously repeating pattern template pattern.
Below is the whole test code:
#include <iostream>
#include <type_traits>
using namespace std;
template<typename Type>
class FunContainer {
protected:
int container_stuff = 0;
public:
int get_container_stuff() {return container_stuff;};
void set_container_stuff(int stuff) {container_stuff = stuff;};
bool is_copyable() {return is_copy_constructible<Type>::value;};
};
class Fun : public FunContainer<Fun> {
public:
std::string str = "Tastic";
Fun() = default;
Fun(const Fun& other_fun) : FunContainer<Fun>(other_fun) {
copy_internals(other_fun);
};
Fun& operator=(const Fun& other_fun){
FunContainer<Fun>::operator=(other_fun);
copy_internals(other_fun);
return *this;
};
private:
void copy_internals(const Fun& other_fun) {str = other_fun.str;};
};
static_assert(is_copy_constructible<Fun>::value, "Type must be copy constructible!");
int main() {
Fun fun;
fun.set_container_stuff(10);
fun.str = "test";
Fun tastic(fun);
cout << tastic.get_container_stuff() << '\n';
cout << tastic.str << '\n';
cout << tastic.is_copyable() << '\n';
return 0;
}
The result will be as expected:
10
test
1
This means that the second statement has passed. It seems that FunCopyConstructible is not inside FunContainer. However, he is_copyablesays that is so.
is_copyable(), <-27 > in-class. . copyable 0;
, - .
Q1 false " " true? , (Fun) / ?
Q2 ? (, is_copy_constructible , " " ).
Q3 , , CopyConstructible ?
clang 3.2-11, gcc 4.8.2 ideone default ++ 11 .