// ---------------------
// InstanceVariables.c++
// ---------------------
#include <cassert> // assert
#include <iostream> // cout, endl
template <typename T>
struct A {
T v;
const T cv;};
template <typename T>
struct B {
T v;
T w;};
int main () {
using namespace std;
cout << "InstanceVariables.c++" << endl;
{
// A<int> x; // error: structure "x" with uninitialized const members
// A<int> x = {2}; // error: uninitialized const member "A<int>::cv"
A<int> x = {2, 3};
assert(x.v == 2);
assert(x.cv == 3);
{
const A<int> y = x; // const A<int> y(x);
assert(y.v == 2);
assert(y.cv == 3);
assert(&x.v != &y.v);
assert(&x.cv != &y.cv);
}
{
A<int> y = {5, 6};
assert(y.v == 5);
assert(y.cv == 6);
// y = x; // error: non-static const member "const int A<int>::cv", can't use default assignment operator
}
}
{
const B<int> x = {2, 3};
assert(x.v == 2);
assert(x.w == 3);
B<int> y = {4, 5};
assert(y.v == 4);
assert(y.w == 5);
y = x;
assert(y.v == 2);
assert(y.w == 3);
assert(&x.v != &y.v);
assert(&x.w != &y.w);
}
cout << "Done." << endl;
return 0;}
syntax highlighted by Code2HTML, v. 0.9.1