https://github.com/goblinhack/c-plus-plus-nested-template-example
c++ template nested
https://github.com/goblinhack/c-plus-plus-nested-template-example
Last synced: 4 months ago
JSON representation
c++ template nested
- Host: GitHub
- URL: https://github.com/goblinhack/c-plus-plus-nested-template-example
- Owner: goblinhack
- Created: 2020-05-12T22:04:49.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2020-05-12T22:12:38.000Z (almost 6 years ago)
- Last Synced: 2025-08-27T09:42:16.614Z (6 months ago)
- Size: 1000 Bytes
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# c-plus-plus-nested-template-example
Place holder for a useful snippet, I invariably always struggle to
get this syntax correct. Hope it helps someone else.
```C++
#include
#include
template class C>
class C2 {
private:
typedef C > C1;
C1 container;
public:
void push(T& item) {
container.push_back(item);
}
T pop (void) {
T item = container.front();
container.pop_front();
return item;
}
};
int main() {
int i = 1;
C2 c;
c.push(i);
std::cout << c.pop() << std::endl;
}
```
And the equivalent to the above, but using value_type
```C++
#include
#include
template
class C1 {
private:
T container;
typedef typename T::value_type CT;
public:
void push(CT& item) {
container.push_back(item);
}
CT pop (void) {
CT item = container.front();
container.pop_front();
return item;
}
};
int main() {
int i = 1;
C1 > c;
c.push(i);
std::cout << c.pop() << std::endl;
}
```