doesn't help.
How can I write a templated Wrapper class so
Wrapper<int> my_var; // becomes std::vector<int> my_var
and
Wrapper<int,10> my_var; // becomes std::array<int,10> my_var
?
I don't really need any array/vectors api exposed from Wrapper. So composition is fine.
Probably better ask on SO though.
Let me know if you find out how, the problem is interesting
Probably enable_if would help you to wrap it but is this only requirement for you? So, if there is size for it, it has to be array and otherwise vector right?
What I think you can do is to create marker class that will be a default second template argument. Something like template <typename T1, class T2 = marker_class> Wrapper For Wrapper with a vector and template <typename T1, int N> Wrapper for Wrapper that is array.
I already have some algo which already can use any of them. The algo uses begin/end/size for the containers. Basically the algo implements cyclic buffer and does some arithmetics on it via special public functions. So in general the remaining requirement is still not satisfied, I wanted my code to look neat, and that's what I'm asking above
Thanks, I'll try. I already tried various ways of specialization but none of them ended up well. Your solution looks different though :)
Might not be ended up well, because you need to be careful about which iterator types are supported by both STL containers. If there is no conflict for both, it is OK.
You need to understand the basic concept about “classes” & “templates” You are mixing your thoughts thinking vector as some kind of array!! Well, “vector” is a class; and “‘my_var” is a variable. You do same thing! But your “class” ways; below code will help you. —this-is-what-you-seek— Wrapper<int> my_var; // becomes std::vector<int> my_var — Here is what you should do! —- #include <iostream> template <class X> class Wrapper { X val[2]; public: Wrapper (X x, X y) { val[0]= x; val[1]= y; } void printVal() { std::cout <<"value(x): " <<val[0] \ << std::endl; std::cout<< "value(y): " <<val[1] \ <<std::endl; } }; int main() { Wrapper<int> x(4,5); x.printVal(); Wrapper<std::string> y("a","b"); y.printVal(); return 0; } 😄
template <class X, int _x> Class Wrapper {..}; Will yield.. Wrapper<int, VAL>; 🙃
You do not understand his question.
I think I read it correctly his question, unless otherwise his English is funny😄 He clearly asked about writing the template which should behave like vector so as vector ops are not visible. — How can I write a templated Wrapper class. >>>he intents to write “Wrapper class” Wrapper<int> my_var; // becomes std::vector<int> my_var —and— Wrapper<int,10> my_var;// becomes std::array<int,10> my_var I don't really need any array/vectors api exposed from Wrapper. >>>he intents to encapsulate the vector behind;!that’s what I’m thinking. —his-Quest!— https://t.me/c/1071392087/171383
Then your solution is what he is searching?
Definitely not. I just meant it doesn't matter if the container will encapsulated or inherited
that's not my understanding. If max size is known at compile time we use std::array, if it's not known we use std::vector and use dynamic allocation. How does your solution solve this?
Did you change your statement😄
The idea is the same, I just didn't have my coffee yet and needed to fix my wording to make it clearer.
He’s talking about compiler doesn’t like value parameter! It’s has nothing to do with compile time constants. I guess that’s what you meant??😄 https://t.me/c/1071392087/171430
Обсуждают сегодня