2

I was wondering if its possible to make the following answer more generic, in the sense that the type of the array be templated instead of just unsigned:

I've enclosed the whole thing in a struct like so:

template<typename ArrayType>
struct Array
{
template<ArrayType... args> struct ArrayHolder {
    static const ArrayType data[sizeof...(args)];
};

template<ArrayType... args> 
const ArrayType ArrayHolder<args...>::data[sizeof...(args)] = { args... };

template<size_t N, template<size_t> class F, ArrayType... args> 
struct generate_array_impl {
    typedef typename generate_array_impl<N-1, F, F<N>::value, args...>::result result;
};

template<template<size_t> class F, ArrayType... args> 
struct generate_array_impl<0, F, args...> {
    typedef ArrayHolder<F<0>::value, args...> result;
};

template<size_t N, template<size_t> class F> 
struct generate_array {
    typedef typename generate_array_impl<N-1, F>::result result;
};
};

but I get the following errors:

c++-4.6 -std=c++0x -o test test.cpp
test.cpp:49:17: error: specializing member ‘Array<ArrayType>::ArrayHolder<args>::data’ requires ‘template<>’ syntax
1
  • 1
    When showing compiler errors and code, it is usually quite helpfull to point in the code the exact line that the compiler is complaining about (i.e. which is line 49?) Commented Aug 25, 2011 at 11:20

3 Answers 3

8

It helps if you indent the struct. The problem is that you are defining the data static member variable inside the Array struct. But it should be at namespace scope:

template<typename ArrayType>
struct Array
{
    template<ArrayType... args> struct ArrayHolder {
        static const ArrayType data[sizeof...(args)];
    };

    template<size_t N, template<size_t> class F, ArrayType... args> 
        struct generate_array_impl {
            typedef typename generate_array_impl<N-1, F, F<N>::value, args...>::result result;
        };

    template<template<size_t> class F, ArrayType... args> 
        struct generate_array_impl<0, F, args...> {
            typedef ArrayHolder<F<0>::value, args...> result;
        };

    template<size_t N, template<size_t> class F> 
        struct generate_array {
            typedef typename generate_array_impl<N-1, F>::result result;
        };
};

template<typename ArrayType> template<ArrayType... args> 
        const ArrayType Array<ArrayType>::ArrayHolder<args...>::data[sizeof...(args)] = { args... };
Sign up to request clarification or add additional context in comments.

Comments

-2

Erm... Why don't you just use std::vector? Or if you want a non-resiable array, then use the equivalent type in boost?

Comments

-2

I don't think the expression like 'template ' is a valid usage of C++11 variadic templates.

It can be of two forms as I understand:

  1. template <typename... args> : variable number of generic type parameters
  2. template <int... args> : variable number of integer(non-type) parameters

Refer to this wikipedia article for variadic templates.

1 Comment

Those are template template arguments and they are valid.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.