Category : templates

I’m trying to write a templated class using another templated class. Here’s my code: #include "Tree.hpp" #include "Exceptions.hpp" #include <cmath> #include <ostream> #include <iomanip> #pragma once template<class K, class T> // key & data class AVL_Tree { private: TNode<K,T>* root; TNode<K,T>* left_most; TNode<K,T>* right_most; TNode<K,T>* rotate(TNode<K,T>* to_cycle); TNode<K,T>* rotateLL(TNode<K,T>* A); TNode<K,T>* rotateLR(TNode<K,T>* A); TNode<K,T>* rotateRL(TNode<K,T>* ..

Read more

Suppose I have a template class Foo with two template args and one template member function. I want to make member function specialization while the template class is partial specialization, but the following code compiled failed by g++ template <typename A, typename B> class Foo { public: template<typename C> void bar(); }; template<> template<typename T> ..

Read more

In C++, normally I use header file for functions and variables’ declarations; and I use .cpp file for their definitions. Such as: hello.h void hello_world(); hello.cpp #include <hello.h> void hello_world(){ cout << "Hello world!" << endl; } And in C++ template is Macro 2.0. So template must be defined in the same compile unit of ..

Read more

I’m learning templates. If I mix up the concepts template / template-type / template-argument, please correct me. I’m trying to write a template function that creates an object and returns it. The type of the object comes from the template argument that has to be explicitly specified. result = createObject<ObjectType>(); This object though is supposed ..

Read more

I need to create a vector that will store void functions with different number of parameters. I used variadic templates for this, but it doesn’t work. #include <iostream> #include <functional> template<class…Types> using MyFun = std::function<void(Types…)>; std::vector<MyFun<>> operators; template<class Lambda> void addOperator(Lambda lambda) { operators.emplace_back(lambda); } int main() { auto f1 = [](int a, int b){std::cout<<a+b;}; ..

Read more