Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6953659
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:30:27+00:00 2026-05-27T14:30:27+00:00

I am trying to fill my classes with different code depending on template arguments

  • 0

I am trying to fill my classes with different code depending on template arguments but get a compilation error. My code is like this:

#include <iostream>
#include <string>

struct EmptyType {  };

template<class  arg1=EmptyType, class arg2=EmptyType, class arg3=EmptyType>
class my_class
{
        my_class(){
                std::cout << 3 << std::endl;
        }
    // FILL_MY_CLASS_DEFINE(3)
};
template<class  arg1, class arg2>
class my_class<arg1,arg2,EmptyType>
{
        my_class(){
                std::cout << 2 << std::endl;
        }
    // FILL_MY_CLASS_DEFINE(2)
};
template<class  arg1>
class my_class<arg1,EmptyType,EmptyType>
{
        my_class(){
                std::cout << 1 << std::endl;
        }
};
template<>
class my_class<EmptyType,EmptyType,EmptyType>
{
    // FILL_MY_CLASS_DEFINE(0)
};

int main(int argc, const char *argv[])
{
    my_class<std::string, double, int> a;
    my_class<std::string, int> b;
    my_class<void> c;
        //my_class d;

    return 0;
}

I get lots of errors:

prog.cpp: In function ‘int main(int, const char**)’:
prog.cpp:9: error: ‘my_class<arg1, arg2, arg3>::my_class() [with arg1 = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, arg2 = double, arg3 = int]’ is private
prog.cpp:38: error: within this context
prog.cpp:17: error: ‘my_class<arg1, arg2, EmptyType>::my_class() [with arg1 = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, arg2 = int]’ is private
prog.cpp:39: error: within this context
prog.cpp:25: error: ‘my_class<arg1, EmptyType, EmptyType>::my_class() [with arg1 = void]’ is private
prog.cpp:40: error: within this context

Live code here. So I wonder: is it possible to have different code inside a class when emulating Variadic templates with C++03?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T14:30:28+00:00Added an answer on May 27, 2026 at 2:30 pm

    You need to make the constructors of your class public before they can be instantiated.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some classes and I'm trying to fill the objects of this class.
I'm trying to fill an array with my foreach loop, but I don't get
I am trying to fill object[] with List<string> but I cannot figure out how
I'm am trying to write vba code to fill out web-forms and click buttons
I'm trying to figure out this problem for one of my comp sci classes,
I found this in an error log and am trying to work out how
This may seem like a simple question however i've spent the last hour trying
Im trying to fill a spinner in my application in a different thread. Can
I am trying to fill remote data into picker, but it crashes. here is
I am trying to fill a form in a php application from a C#

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.