Thread: Is that possible to choose classes based on template input?

  1. #1
    Registered User
    Join Date
    Mar 2022
    Posts
    9

    Unhappy Is that possible to choose classes based on template input?

    Hello,

    I'm new to C++ and I need to achieve a way where I need to pass variables via template like below:
    Code:
    Category<box, shelf>(params)
    There are subclasses of Category. where I have three types of Box and 4 types of Shelf and each class contains the different logic, mean total 12 subclasses, I need to select class based on box and shelf input value

    How can I achieve this?

  2. #2
    Registered User
    Join Date
    Apr 2021
    Posts
    139
    Quote Originally Posted by 2k10cs86 View Post
    Hello,

    I'm new to C++ and I need to achieve a way where I need to pass variables via template like below:
    Code:
    Category<box, shelf>(params)
    There are subclasses of Category. where I have three types of Box and 4 types of Shelf and each class contains the different logic, mean total 12 subclasses, I need to select class based on box and shelf input value

    How can I achieve this?
    My first suggestion will be "change your approach!"

    If you have truly different custom implementations for all 12 possibilities, you don't really have a template, do you? Templates are cases where the code is mostly all the same, but some specific details like the payload type are different. If your code is all different, why bother?

    Maybe you should look at polymorphism and virtual methods, and just implement
    Code:
    class Box1Shelf1
    and friends.

    That said, to answer your specific question, you can write value-specialized templates by eliminating the specialized variables from your template declaration and adding the specialization to the class name:

    Code:
    template<bool> struct StaticAssertStruct;
    template<> struct StaticAssertStruct<true> {};
    Note that this is true everywhere! You have to spell the class name as ClassName<specialization> all over the place. It's an incredible pain, which is why I recommend you don't do this.

  3. #3
    Registered User
    Join Date
    Sep 2020
    Posts
    150
    I'm new to C++ and I need to achieve a way where I need to pass variables via template like below:
    You can't pass variables to a template because everything must be known at compile time.
    You specify types or constant numbers, like:
    std::vector<int> v;
    std::array<int, 3> a;

  4. #4
    Registered User
    Join Date
    Mar 2022
    Posts
    9
    Quote Originally Posted by aghast View Post
    My first suggestion will be "change your approach!"

    If you have truly different custom implementations for all 12 possibilities, you don't really have a template, do you? Templates are cases where the code is mostly all the same, but some specific details like the payload type are different. If your code is all different, why bother?

    Maybe you should look at polymorphism and virtual methods, and just implement
    Code:
    class Box1Shelf1
    and friends.

    That said, to answer your specific question, you can write value-specialized templates by eliminating the specialized variables from your template declaration and adding the specialization to the class name:

    Code:
    template<bool> struct StaticAssertStruct;
    template<> struct StaticAssertStruct<true> {};
    Note that this is true everywhere! You have to spell the class name as ClassName<specialization> all over the place. It's an incredible pain, which is why I recommend you don't do this.
    Thanks, it make sense, I have tried to change the approach. but now I'm facing new issue:

    Code:
    // Product.h
    class Product
    {
    };
    
    
    // Category.h
    #include "Product.h"
    
    
    enum MyType
    {
        AAA,
        BBB
    };
    
    
    class Category
    {
    public:
        virtual int getX(Product) = 0;
        virtual void printX(Product) = 0;
    };
    
    
    class CategoryImpl : public Category
    {
    public:
        void printX(Product &p);
    };
    
    
    template <MyType t>
    class CategoryImplType : public CategoryImpl
    {
    };
    
    
    template <>
    class CategoryImplType<AAA> : public CategoryImpl
    {
    public:
        int getX(Product &p);
    };
    template <>
    class CategoryImplType<BBB> : public CategoryImpl
    {
    public:
        int getX(Product &p);
    };
    
    
    // Category.cpp
    #include "Category.h"
    #include "string"
    
    
    void CategoryImpl::printX(Product &p)
    {
        // ...
    }
    int CategoryImplType<AAA>::getX(Product &p)
    {
        return 0;
    }
    int CategoryImplType<BBB>::getX(Product &p)
    {
        return 1;
    }
    
    
    // main.cpp
    #include "Category.h"
    
    using namespace std;
    
    int main()
    {
        CategoryImplType<AAA>(); // Error on this line shown below:
        /* template<> class CategoryImplType<AAA>
           a cast to abstract class "CategoryImplType<AAA>" is not allowed:C/C++(389)
           main.cpp(8, 5): pure virtual function "Category::getX" has no overrider
           main.cpp(8, 5): pure virtual function "Category::printX" has no overrider
        */
    };
    Last edited by 2k10cs86; 03-05-2022 at 03:05 PM.

  5. #5
    Registered User
    Join Date
    Sep 2020
    Posts
    150
    I have difficulty understanding what you want to do.
    It seems you want to implement different categories, but how do they differ. ?
    Implementing a product with a category shouldn't be so complicated. What exactly do you want to do ?

  6. #6
    Registered User
    Join Date
    Mar 2022
    Posts
    9
    Quote Originally Posted by thmm View Post
    I have difficulty understanding what you want to do.
    It seems you want to implement different categories, but how do they differ. ?
    Implementing a product with a category shouldn't be so complicated. What exactly do you want to do ?
    I want to select class based on template constant, rather than using if condition. In actual my classes name are different so here I have used different names here.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 09-13-2013, 04:13 PM
  2. Template/classes/strings
    By llVIU in forum C++ Programming
    Replies: 21
    Last Post: 05-25-2011, 03:48 PM
  3. Replies: 9
    Last Post: 04-06-2011, 01:37 PM
  4. Specifying Allowed Template Classes
    By nine-hundred in forum C++ Programming
    Replies: 3
    Last Post: 08-30-2007, 12:22 AM
  5. Template Classes
    By AdioKIP in forum C++ Programming
    Replies: 5
    Last Post: 02-24-2002, 06:13 PM

Tags for this Thread