-2

The question is simple - I wasn't able to find a better example in the ISO C++ documentation then this:

template<class ... targs> void func(targs ... args);

But even for it I wasn't able to find a satisfying explanation. And this is not what I'm looking for as I don't care for the argument types.

I want to write something like this:

void func(auto ... args)
{
    func1(args...); //here 'args' to be expanded
}

But the above or other similar representations won't compile under Clang.

Any ideas why and how can it be done? Also a quote from the latest ISO C++ standard will be welcome too.

What I want to do is implementing 'new' operator behavior by my own function and for this I need to pass constructor arguments with variable size, something like this:

struct x_new
{
    template<class T>
    inline T *allocate(... arg)
    {
        return new T(arg...);
    }
};

Which won't compile under Clang for now and I don't know why.

AnArrayOfFunctions
  • 3,452
  • 2
  • 29
  • 66
  • 2
    clang has no problems with `template void func(targs ... args);`. And it happily accepts the body you've written for it too. What exactly is your question? –  Mar 07 '15 at 10:34
  • This is not a single named parameter pack but something else. What is it? Also why I can't write the second one? – AnArrayOfFunctions Mar 07 '15 at 10:38
  • What are you trying to do? Can you show how you want to use your function? What problem are you actually trying to solve? Related reading: [What is the XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). – Some programmer dude Mar 07 '15 at 10:40
  • 1
    You seem to somehow confuse a template function with variadic arguments (which is really a whole number of functions, instantiated depending on what you call the function with) and C-style varargs. You also seem to make some funny assumptions on `auto`. Note that `auto` is no such thing as "magic type removal", it has very clear (and strict) rules. If you write `auto`, the compiler needs a means to derive the correct type, which isn't the case here. – Damon Mar 07 '15 at 10:41
  • "named parameter pack" is not a standard term and I have no idea what you mean by it. `args` is a parameter pack, and it looks named to me, so why isn't it a named parameter pack? As for what's wrong with the second, clang's message seems clear to me: you just can't have parameters declared `auto`. –  Mar 07 '15 at 10:41
  • If I understand your intent, what you want is something like: `templatevoid func(args...) { func2(std::forward(args...)); }` ? – Damon Mar 07 '15 at 10:44
  • I updated my OP to shows my indent. – AnArrayOfFunctions Mar 07 '15 at 10:48

1 Answers1

1

How about using normal template parameter packs like usual? Like e.g.

template<typename T, typename... argsT>
T *allocate(argsT... args)
{
    return new T(std::forward<argsT>(args)...);
}
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • 1
    Better yet, use `std::forward`. But yeah, this use of parameter packs is right. Worth noting might be that calling `allocate` still allows `argsT` to be deduced from the actual arguments, it doesn't specify a fixed-but-empty set for the pack. –  Mar 07 '15 at 10:58
  • The way to use `std::forward` is `std::forward(args)...`. You need to specify the template argument because it cannot be meaningfully deduced from `args`, as they will always be lvalues. The `...` needs to be outside the invocation because you need `std::forward` to be called for each parameter. It will appear to work okay so long as you only have a single parameter, but will break if you have more, or if you have none. –  Mar 07 '15 at 11:07
  • @hvd Thanks, I'm not fully up to date with all the new stuff yet. – Some programmer dude Mar 07 '15 at 11:10
  • Also I would like a citation from the ISO C++ standard which explains what is this and why my second example is invalid. – AnArrayOfFunctions Mar 07 '15 at 11:41