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 7402213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T04:41:10+00:00 2026-05-29T04:41:10+00:00

I have the following class template: template<class T, unsigned N> class MyClass; where T

  • 0

I have the following class template:

template<class T, unsigned N>
class MyClass;

where T is some type, N – number of components. It is possible to initialize the class using MyClass{a1, a2, a3} where the number of arguments is equal to N.

I want to add a member function template (let’s name it foo) of MyClass that would meet the following requirements:

  1. It is templated by another type T2 (i.e. template<class T2> void foo(..))
  2. It accepts enough data to construct MyClass<T,N>, but not less and not more. Violating this results in a compile-time error.
  3. It deduces T2 from the types of the parameters. I.e. I want that it would be possible to call foo({a1, a2, a3}) or foo(a1, a2, a3) or similar, without typing <double> or MyClass<double,N> every time.

Is there a way to implement the function so that the above requirements are satisfied?

I’ve already thought about and/or tried the following solutions:

1) The obvious one:

...
template<class T2>
void foo(MyClass<T2, N> arg);  
...
a.foo({1,2,3}); //compile-time error

Can’t work in principle, because braced initializer lists are a non-deduced context, thus they can’t deduce any types. That’s quite unfortunate, I’d be very happy if this worked.

2) initializer_list

Can’t work in principle, because it can’t check the number of arguments at compile-time.

3) Variadic template magic

Something like the function below would be neat:

template<class...T2, class std::enable_if<sizeof...(T2) == N, int>::type = 0>
void foo(T2... args);
..
foo(1,2,3);

However, I couldn’t get it to work – T2 still couldn’t be deduced. Maybe someone knows why? I used GCC4.7 20120121 snapshot.

4) The ugly one

Essentially this is the same as the above one, just expanded into several overloads for different N. I would better reimplement MyClass as a set of specializations for different Ns than to use this one.

template<class T2, class std::enable_if<N == 1, int>::type = 0>
void fun(T2 a1); //if N == 1
template<class T2, ..>
void fun(T2 a1, T2 a2); //if N == 2
template<class T2, ..>
void fun(T2 a1, T2 a2, T2 a3); //if N == 3
...
  • 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-29T04:41:12+00:00Added an answer on May 29, 2026 at 4:41 am

    Your third variant’s second non-type param should have prefix typename not class :

    template<class...T2, typename std::enable_if<sizeof...(T2) == N, int>::type = 0>
    void foo(T2... args);
    ..
    foo(1,2,3);
    

    Check it

    Gcc 4.7.0 snapshots has some bugs with templates I guess, if you try it with gcc 4.6.2/1 it shall work.

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

Sidebar

Related Questions

I have the following problem using template instantiation [*]. file foo.h class Foo {
I have the following pattern: template <int a, int b> class MyClass { public:
I have the following template class and a (global) variable of its type: template
I have the following class definition: template<typename QueueItemT> class QueueBC { protected: QueueBC() {}
I have the following code: class TimeOutException {}; template <typename T> class MultiThreadedBuffer {
I have the following use case: There's a class called Template and with that
I have the following code: template <class T> struct pointer { operator pointer<const T>()
Currently ive got some reference counted classes using the following: class RefCounted { public:
I have the following template structure: template <typename scalar_type> struct postc_params{ scalar_type delta; unsigned
I have the following class CppProperty class that holds value: template<typename TT> class CppProperty

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.