Get templated struct without knowing initial template arguments

I am looking for a way to get a specified templated struct from the custom Node classes I am building without having to static_cast or dynamic_cast when I get the pointers. What ways can I use to avoid that?

node.hpp

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>#pragma once
#ifndef NODE_H
#define NODE_H
// for ease of use
#ifndef DATA_TEMPLATE
#define DATA_TEMPLATE template<typename T>
#else
#define DATA_TEMPLATE
#endif
#ifndef FUNCTION_TEMPLATE
#define FUNCTION_TEMPLATE template<typename R, typename... T>
#else
#define FUNCTION_TEMPLATE
#endif
#include <iostream>
#include <string>
#include <functional>
#include <ostream>
#include <memory>
struct Node {
Node* prev;
Node* next;
Node(Node* prev = nullptr, Node* next = nullptr) : prev(prev), next(next) {}
virtual ~Node() = default;
virtual Node* getNext() const { return next; }
virtual void setNext(Node* next) { this->next = next; }
virtual Node* getPrev() const { return prev; }
virtual void setPrev(Node* prev) { this->prev = prev; }
virtual void print(std::ostream& os) const = 0;
};
DATA_TEMPLATE
struct DataNode : public Node {
T data;
DataNode(Node* prev, T dat, Node* next) : Node{ prev, next }, data(dat) {}
DataNode(Node* prev, T dat) : DataNode{ prev, dat, nullptr } {}
DataNode(T dat, Node* next) : DataNode{ nullptr, dat, next } {}
DataNode(T dat) : DataNode{ nullptr, dat, nullptr } {}
T getData() { return data; }
void setData(T newData) { data = newData; }
void print(std::ostream& os) const override { os << data; }
friend std::ostream& operator<<(std::ostream& os, const DataNode<T>& dn) { dn.print(os); return os; };
};
FUNCTION_TEMPLATE
struct FunctionNode : public Node {
std::function<R(T...)> fn;
FunctionNode(Node* prev, std::function<R(T...)> func, Node* next) : Node{ prev, next }, fn(func) {}
FunctionNode(Node* prev, std::function<R(T...)> func) : FunctionNode{ prev, nullptr, func } {}
FunctionNode(std::function<R(T...)> func, Node* next) : FunctionNode{ nullptr, next, func } {}
FunctionNode(std::function<R(T...)> func) : FunctionNode{ nullptr, nullptr, func } {}
R operator()(T... args) { return fn(args...); }
};
#endif
</code>
<code>#pragma once #ifndef NODE_H #define NODE_H // for ease of use #ifndef DATA_TEMPLATE #define DATA_TEMPLATE template<typename T> #else #define DATA_TEMPLATE #endif #ifndef FUNCTION_TEMPLATE #define FUNCTION_TEMPLATE template<typename R, typename... T> #else #define FUNCTION_TEMPLATE #endif #include <iostream> #include <string> #include <functional> #include <ostream> #include <memory> struct Node { Node* prev; Node* next; Node(Node* prev = nullptr, Node* next = nullptr) : prev(prev), next(next) {} virtual ~Node() = default; virtual Node* getNext() const { return next; } virtual void setNext(Node* next) { this->next = next; } virtual Node* getPrev() const { return prev; } virtual void setPrev(Node* prev) { this->prev = prev; } virtual void print(std::ostream& os) const = 0; }; DATA_TEMPLATE struct DataNode : public Node { T data; DataNode(Node* prev, T dat, Node* next) : Node{ prev, next }, data(dat) {} DataNode(Node* prev, T dat) : DataNode{ prev, dat, nullptr } {} DataNode(T dat, Node* next) : DataNode{ nullptr, dat, next } {} DataNode(T dat) : DataNode{ nullptr, dat, nullptr } {} T getData() { return data; } void setData(T newData) { data = newData; } void print(std::ostream& os) const override { os << data; } friend std::ostream& operator<<(std::ostream& os, const DataNode<T>& dn) { dn.print(os); return os; }; }; FUNCTION_TEMPLATE struct FunctionNode : public Node { std::function<R(T...)> fn; FunctionNode(Node* prev, std::function<R(T...)> func, Node* next) : Node{ prev, next }, fn(func) {} FunctionNode(Node* prev, std::function<R(T...)> func) : FunctionNode{ prev, nullptr, func } {} FunctionNode(std::function<R(T...)> func, Node* next) : FunctionNode{ nullptr, next, func } {} FunctionNode(std::function<R(T...)> func) : FunctionNode{ nullptr, nullptr, func } {} R operator()(T... args) { return fn(args...); } }; #endif </code>
#pragma once

#ifndef NODE_H
#define NODE_H

// for ease of use
#ifndef DATA_TEMPLATE
#define DATA_TEMPLATE template<typename T>
#else
#define DATA_TEMPLATE
#endif

#ifndef FUNCTION_TEMPLATE
#define FUNCTION_TEMPLATE template<typename R, typename... T>
#else
#define FUNCTION_TEMPLATE
#endif

#include <iostream>
#include <string>
#include <functional>
#include <ostream>
#include <memory>

struct Node {
    Node* prev;
    Node* next;

    Node(Node* prev = nullptr, Node* next = nullptr) : prev(prev), next(next) {}

    virtual ~Node() = default;
    virtual Node* getNext() const { return next; }
    virtual void setNext(Node* next) { this->next = next; }
    virtual Node* getPrev() const { return prev; }
    virtual void setPrev(Node* prev) { this->prev = prev; }

    virtual void print(std::ostream& os) const = 0;
};

DATA_TEMPLATE
struct DataNode : public Node {
    T data;

    DataNode(Node* prev, T dat, Node* next) : Node{ prev, next }, data(dat) {}
    DataNode(Node* prev, T dat) : DataNode{ prev, dat, nullptr } {}
    DataNode(T dat, Node* next) : DataNode{ nullptr, dat, next } {}
    DataNode(T dat) : DataNode{ nullptr, dat, nullptr } {}

    T getData() { return data; }
    void setData(T newData) { data = newData; }

    void print(std::ostream& os) const override { os << data; }

    friend std::ostream& operator<<(std::ostream& os, const DataNode<T>& dn) { dn.print(os); return os; };
};

FUNCTION_TEMPLATE
struct FunctionNode : public Node {
    std::function<R(T...)> fn;

    FunctionNode(Node* prev, std::function<R(T...)> func, Node* next) : Node{ prev, next }, fn(func) {}
    FunctionNode(Node* prev, std::function<R(T...)> func) : FunctionNode{ prev, nullptr, func } {}
    FunctionNode(std::function<R(T...)> func, Node* next) : FunctionNode{ nullptr, next, func } {}
    FunctionNode(std::function<R(T...)> func) : FunctionNode{ nullptr, nullptr, func } {}

    R operator()(T... args) { return fn(args...); }
};

#endif

main.cpp

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>#include <iostream>
#include <string>
#include <stdexcept>
#include "node.hpp"
// Some testing functions for the FunctionNode struct
int add(int x, int y) {
return x + y;
}
double divideNums(double x, double y) {
if (y > 0) {
return x / y;
} else {
throw std::runtime_error("Zero Division Error: cannot divide by zero!");
}
}
int main()
{
DataNode<int> node{
new DataNode(7),
3,
new DataNode{5}
};
std::cout << node << std::endl;
Node* prevNode = node.getPrev();
Node* nextNode = node.getNext();
// Takes the previous and next nodes and checks if they can be dynamically
// cast to a DataNode pointer with the template argument "int".
// I want to avoid this if the user doesn't know what initial arguments there were
// if possible
if (auto* pn = dynamic_cast<DataNode<int>*>(prevNode))
std::cout << *pn << std::endl;
else
std::cout << "Node is not a DataNode" << std::endl;
if (auto* nn = dynamic_cast<DataNode<int>*>(nextNode))
std::cout << *nn << std::endl;
else
std::cout << "Node is not a DataNode" << std::endl;
return 0;
}
</code>
<code>#include <iostream> #include <string> #include <stdexcept> #include "node.hpp" // Some testing functions for the FunctionNode struct int add(int x, int y) { return x + y; } double divideNums(double x, double y) { if (y > 0) { return x / y; } else { throw std::runtime_error("Zero Division Error: cannot divide by zero!"); } } int main() { DataNode<int> node{ new DataNode(7), 3, new DataNode{5} }; std::cout << node << std::endl; Node* prevNode = node.getPrev(); Node* nextNode = node.getNext(); // Takes the previous and next nodes and checks if they can be dynamically // cast to a DataNode pointer with the template argument "int". // I want to avoid this if the user doesn't know what initial arguments there were // if possible if (auto* pn = dynamic_cast<DataNode<int>*>(prevNode)) std::cout << *pn << std::endl; else std::cout << "Node is not a DataNode" << std::endl; if (auto* nn = dynamic_cast<DataNode<int>*>(nextNode)) std::cout << *nn << std::endl; else std::cout << "Node is not a DataNode" << std::endl; return 0; } </code>
#include <iostream>
#include <string>
#include <stdexcept>
#include "node.hpp"

// Some testing functions for the FunctionNode struct
int add(int x, int y) {
    return x + y;
}

double divideNums(double x, double y) {
    if (y > 0) {
        return x / y;
    } else {
        throw std::runtime_error("Zero Division Error: cannot divide by zero!");
    }
}

int main()
{
    DataNode<int> node{
        new DataNode(7),
        3,
        new DataNode{5}
    };

    std::cout << node << std::endl;

    Node* prevNode = node.getPrev();
    Node* nextNode = node.getNext();

        // Takes the previous and next nodes and checks if they can be dynamically
        // cast to a DataNode pointer with the template argument "int".
        // I want to avoid this if the user doesn't know what initial arguments there were
        // if possible
    if (auto* pn = dynamic_cast<DataNode<int>*>(prevNode))
        std::cout << *pn << std::endl;
    else
        std::cout << "Node is not a DataNode" << std::endl;

    if (auto* nn = dynamic_cast<DataNode<int>*>(nextNode))
        std::cout << *nn << std::endl;
    else
        std::cout << "Node is not a DataNode" << std::endl;

    return 0;
}

I want to do what I am doing in main.cpp without having to use dynamic cast, such as

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>#include <iostream>
#include <string>
#include <stdexcept>
#include "node.hpp"
// Some testing functions for the FunctionNode struct
int add(int x, int y) {
return x + y;
}
double divideNums(double x, double y) {
if (y > 0) {
return x / y;
} else {
throw std::runtime_error("Zero Division Error: cannot divide by zero!");
}
}
int main()
{
// A DataNode with a previous and next node, and a data value of 3
DataNode<int> node{
new DataNode(7),
3,
new DataNode{5}
};
std::cout << node << std::endl; // prints "3" to the console
// Gets the previous and next nodes respectively with their given types
// and template arguments
auto* prevNode = node.getPrev();
auto* nextNode = node.getNext();
// So I can use the nodes as such
std::cout << prevNode << std::endl; // prints "7" to the console
std::cout << nextNode << std::endl; // prints "5" to the console
return 0;
}
</code>
<code>#include <iostream> #include <string> #include <stdexcept> #include "node.hpp" // Some testing functions for the FunctionNode struct int add(int x, int y) { return x + y; } double divideNums(double x, double y) { if (y > 0) { return x / y; } else { throw std::runtime_error("Zero Division Error: cannot divide by zero!"); } } int main() { // A DataNode with a previous and next node, and a data value of 3 DataNode<int> node{ new DataNode(7), 3, new DataNode{5} }; std::cout << node << std::endl; // prints "3" to the console // Gets the previous and next nodes respectively with their given types // and template arguments auto* prevNode = node.getPrev(); auto* nextNode = node.getNext(); // So I can use the nodes as such std::cout << prevNode << std::endl; // prints "7" to the console std::cout << nextNode << std::endl; // prints "5" to the console return 0; } </code>
#include <iostream>
#include <string>
#include <stdexcept>
#include "node.hpp"

// Some testing functions for the FunctionNode struct
int add(int x, int y) {
    return x + y;
}

double divideNums(double x, double y) {
    if (y > 0) {
        return x / y;
    } else {
        throw std::runtime_error("Zero Division Error: cannot divide by zero!");
    }
}

int main()
{
        // A DataNode with a previous and next node, and a data value of 3
    DataNode<int> node{
        new DataNode(7),
        3,
        new DataNode{5}
    };

    std::cout << node << std::endl; // prints "3" to the console

        // Gets the previous and next nodes respectively with their given types
        // and template arguments
    auto* prevNode = node.getPrev();
    auto* nextNode = node.getNext();

        // So I can use the nodes as such
        std::cout << prevNode << std::endl; // prints "7" to the console
        std::cout << nextNode << std::endl; // prints "5" to the console

    return 0;
}

I’ve tried using the “auto” keyword, though I know that isn’t a proper solution, maybe. I’ve tried a few things to avoid the extra lines of code to no avail.

I can get the Nodes, but outputting them just gives me this (Output from main.cpp)

3

00000232104D8850

00000232104D8130

Which is the value of the main node, but the previous and next nodes addresses only

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật