Похожие чаты

Hi. I got stuck with some simple thing and google

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.

18 ответов

6 просмотров

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.

Sergey- Автор вопроса
Cengizhan Pasaoglu
Probably enable_if would help you to wrap it but i...

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

Sergey- Автор вопроса
Ihor
What I think you can do is to create marker class ...

Thanks, I'll try. I already tried various ways of specialization but none of them ended up well. Your solution looks different though :)

Sergey
Thanks, I'll try. I already tried various ways of ...

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; } 😄

Aakash Saini
You need to understand the basic concept about “cl...

template <class X, int _x> Class Wrapper {..}; Will yield.. Wrapper<int, VAL>; 🙃

Cengizhan Pasaoglu
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

Sergey- Автор вопроса
Cengizhan Pasaoglu
Then your solution is what he is searching?

Definitely not. I just meant it doesn't matter if the container will encapsulated or inherited

Aakash Saini
I think I read it correctly his question, unless o...

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?

Aakash Saini
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.

Ihor
that's not my understanding. If max size is known ...

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

Похожие вопросы

Обсуждают сегодня

Господа, а что сейчас вообще с рынком труда на делфи происходит? Какова ситуация?
Rꙮman Yankꙮvsky
29
А вообще, что может смущать в самой Julia - бы сказал, что нет единого стандартного подхода по многим моментам, поэтому многое выглядит как "хаки" и произвол. Короче говоря, с...
Viktor G.
2
@Benzenoid can you tell me the easiest, and safest way to bu.y HEX now?
Živa Žena
20
This is a question from my wife who make a fortune with memes 😂😂 About the Migration and Tokens: 1. How will the old tokens be migrated to the new $LGCYX network? What is th...
🍿 °anton°
2
30500 за редактор? )
Владимир
47
а через ESC-код ?
Alexey Kulakov
29
What is the Dex situation? Agora team started with the Pnetwork for their dex which helped them both with integration. It’s completed but as you can see from the Pnetwork ann...
Ben
1
Гайс, вопрос для разносторонее развитых: читаю стрим с юарта, нада выделять с него фреймы с определенной структурой, если ли чо готовое, или долбаться с ринг буффером? нада у...
Vitaly
9
Anyone knows where there are some instructions or discort about failed bridge transactions ?
Jochem
21
@lozuk how do I get my phex copies of my ehex from a atomic wallet, to move to my rabby?
Justfrontin 👀
11
Карта сайта