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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:19:38+00:00 2026-06-12T19:19:38+00:00

I have template class Reader template<typename T> class Reader{ typedef T type; }; Special

  • 0

I have template class Reader

template<typename T>
class Reader{
    typedef T type;
};

Special implementations (derrived classes) have methods with signature

T read(IStream&, any number of arguments, zero possible)

i.e class IntegerReader public functions:

template <typename T>
class IntegerReader : public Reader<T>{
public:
    T read(IStream& stream);
    T read(IStream& stream, T min, T max);
    T read(IStream& stream, T min, T max, std::string name);
}

Now I want to create a wrapper, that will allow me create another reader, with will call some reader with arguments.

I’ve tried this:

template <typename T, typename... Args>
class ParametrizedReader : public Reader<typename T::type> {
    T reader;
    Args... args;
    ParametrizedReader(T reader, Args... args):reader(reader), args(args){
    }

    typename T::type read(IStream& stream){
        return reader.read(args..., stream);
    }
};
testlib/readerWrapper.hpp:7:6: error: expected unqualified-id before ‘...’ token
testlib/readerWrapper.hpp: In constructor ‘ParametrizedReader<T, Args>::ParametrizedReader(T, Args ...)’:
testlib/readerWrapper.hpp:8:61: error: class ‘ParametrizedReader<T, Args>’ does not have any field named ‘args’
testlib/readerWrapper.hpp: In member function ‘typename T::type ParametrizedReader<T, Args>::read(IStream&)’:
testlib/readerWrapper.hpp:12:22: error: ‘args’ was not declared in this scope

this:

template <typename T, typename... Args>
class ParametrizedReader : public Reader<typename T::type> {
    std::function<T()> lambda;
    ParametrizedReader(T reader, Args... args){
        lambda = [=](IStream& stream){
            reader.read(stream, args...);
        };
    }

    typename T::type read(IStream& stream){
        return lambda(stream);
    }
};

testlib/readerWrapper.hpp:9:24: error: parameter packs not expanded with ‘...’:
testlib/readerWrapper.hpp:9:24: note:         ‘args’
testlib/readerWrapper.hpp:9:28: error: expansion pattern ‘args’ contains no argument packs

and this:

template <typename T, typename... Args>
class ParametrizedReader : public Reader<typename T::type> {
    std::function<T()> lambda;
    ParametrizedReader(T reader, Args... args){
        lambda = [reader, args...](IStream& stream){
            reader.read(stream, args...);
        };
    }

    typename T::type read(IStream& stream){
        return lambda(stream);
    }
};

testlib/readerWrapper.hpp:8:25: error: expected ‘,’ before ‘...’ token
testlib/readerWrapper.hpp:8:25: error: expected identifier before ‘...’ token
testlib/readerWrapper.hpp:8:28: error: parameter packs not expanded with ‘...’:
testlib/readerWrapper.hpp:8:28: note:         ‘args’

Compilation errors given by g++-4.7

While I’m not sure the first example is correct and should compile, I believe the second and third should.

I found this bug, which seems to be not fixed.

Is there workarounds, how can I do what I want?

  • 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-06-12T19:19:40+00:00Added an answer on June 12, 2026 at 7:19 pm

    You can work around this problem by binding the arguments to the lambda, and not capturing them.

    ParametrizedReader(T reader, Args... args){
        lambda = std::bind(
            [=](IStream& stream, Args... as){
                reader.read(stream, as...);
            }, args...);
    }
    

    Though you might want to do as @Alexandre says and not parameterize the class template on the exact argument types:

    template <typename T>
    class ParametrizedReader : public Reader<typename T::type> {
        std::function<T()> lambda;
        template<typename... Args>
        ParametrizedReader(T reader, Args... args){
            lambda = std::bind(
                [=](IStream& stream, Args... as){
                    reader.read(stream, as...);
                }, args...);
        }
        // ...
    };
    

    (Note: Untested.)

    What might also work is to just drop std::bind in the second snippet and try like your second or third solution, this time with a variadic function template. Maybe it works, who knows.

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

Sidebar

Related Questions

I have a template class, the only template parameter is typename. template<typename T> class
I have a jquery template which renders pictures <script id=PictureList type=text/x-jquery-tmpl> <ul class=pictureList> <li
I have a template class, called OneCell , here the definition: template <class T>
I have a template class method like that: template<class T> static tmpClass<T>* MakeInstance(T value)
I have a template class, called Cell , here the definition: template <class T>
I have a template class: template<class T> class A{ T a, b; public: A(A<T>
I have a template class defined like so: template <class T> class Command {
I have non-template class with a templatized constructor. This code compiles for me. But
I have the template class and array of pointers to objects and overloaded logic
I have a template class with a variadic template member function that I am

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.