Created
October 10, 2014 19:23
-
-
Save chrisdembia/7ac3f5d00c8e51a952f7 to your computer and use it in GitHub Desktop.
Ajay: templatized in-class member initializers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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