Skip to content

Instantly share code, notes, and snippets.

@chrisdembia
Created October 10, 2014 19:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisdembia/7ac3f5d00c8e51a952f7 to your computer and use it in GitHub Desktop.
Save chrisdembia/7ac3f5d00c8e51a952f7 to your computer and use it in GitHub Desktop.
Ajay: templatized in-class member initializers
#include <iostream>
#include <vector>
template <int M>
class myvec {
public:
std::vector<double> v;
myvec(double iv) : v(M, iv) {}
};
class A {
protected:
int init(int i) {
return i;
}
// template <typename T>
// T val(int n) { return v; }
template <int M>
std::vector<double> create_vec(double iv) {
return std::vector<double>(M, iv);
}
template <int M>
myvec<M> create_myvec(double iv) {
return myvec<M>(iv);
}
};
class B : public A {
public:
int initB() { return 3; }
int j = init(5);
int k = initB();
// std::vector<int> z {val(8);
std::vector<double> v { create_vec<10>(4) };
myvec<10> w { create_myvec<10>(6) };
};
int main() {
B b;
std::cout << b.j << b.k << std::endl;
for (auto entry : b.v)
std::cout << entry << std::endl;
for (auto entry : b.w.v)
std::cout << entry << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment