Title: C++ templates with various types based on input
Tags: c++;templates
Question: I know there are many questions about that issue but nothing seems to work for me or it's too complex for me to understand.
So I have template Node
template <typename T&gt;
class Node {
public:
T value;
Node* right;
Node* left;
Node(T value, Node<T&gt;* right, Node<T&gt;* left);
And Tree
template <typename T&gt;
class Tree {
public:
Node<T&gt;* root;
Tree();
~Tree() {}
void insert(T value);
Node<T&gt;* search(T value, Node<T&gt;* root) noexcept(false);
Now I want to create different Tree templates basing on what the user chose. Users can choose int, double, or string, these are the only options. I tried to use base class solution but my problem is that Tree uses type T in functions (and in Node) so I don't know how I should declare them in, let's call it BaseTree. Then I would be able to something like this:
BaseTree* tree;
tree = new Tree<int&gt;();
I'm looking for a simple solution, I'm sort of beginner and it surprises me how this simple issue is so difficult for me to solve.
Comment: Your `Tree` type is templated by a type `T`, so any code that interacts with one should either specify `T` or be themselves templated by `T` and use `Tree`.


Here is another answer: The solution is to templatize the generic functions also. Let's say you have something like this in mind:
void traverse(BaseTree const&amp; tree) noexcept {
/* Logic here */
Instead, you do:
template<typename T&gt;
void traverse(Tree<T&gt; const&amp; tree) noexcept {
/* Logic here */
This also helps that you can use Node<T&gt; to refer to the node instead of fabricating BaseNode and whatnot again.
Comment for this answer: Thank you for the answer! It helped me a lot, though I have another issuen now... In my BaseTree class I have ```template
Node* search(T value, Node* root) noexcept(false);```,  and when I try to call this function from main, like this `tree->search(10, tree->root)` it says root is not a member function of BaseTree but I can't add `Node* root` there. I get ` data member ‘root’ cannot be a member template
Node* root;`. Do you have any idea if there's a way to fix this?
Comment for this answer: Okay, I sloved previous problem, now I get ` undefined reference to `void BaseTr75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0insert(int)'`
Comment for this answer: @Awerde if you are declaring a member function, you don't have to template it. You don't even need the `root` argument: https://gcc.godbolt.org/z/gCeF8e

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 1, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2020:00:05/20/20', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<c++><templates>', 'Title': 'C++ templates with various types based on input', 'ViewCount': 68, 'id': 61912966, 'user_AboutMe': None, 'user_AccountId': None, 'user_DisplayName': None, 'user_Reputation': None, 'user_Views': None, 'user_id': None}}