Variadic template parameters of one specific type

半城伤御伤魂 提交于 2019-12-09 17:53:54

问题


Why there is no specific type allowed in a variadic template pack?

template< typename T >
class Foo
{
public:
    template< typename... Values >
    void bar( Values... values )
    {
    }

    template< T... values >            <-- syntax error
    void bar( T... values )
    {
    }

    template< int... values >          <-- syntax error
    void bar( int... values )
    {
    }
};

Whats the rationale in not allowing this?
Are there proposals for this?


Note: alternatives would be

  • std::initializer_list< T > without narrowing of types and the { }-brace-syntax
  • a (ugly) recursive trait that checks all types seperately: see here

回答1:


It IS allowed, actually, you're just using it wrong. T... and int... are non-type parameter packs and their elements are values, so you can't use them as type specifiers (and you can't deduce them from a function call).

An example of correct usage:

template<int... Is>
struct IntPack {};

IntPack<1,2,3> p;

or

template< typename T >
struct Foo
{
    template< T... Ts>
    void bar()
    {
    }
};

int main()
{
    Foo<int> f;
    f.bar<1,2,3>();
}

Another example would be std::integer_sequence.



来源:https://stackoverflow.com/questions/30773216/variadic-template-parameters-of-one-specific-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!