1

This code has a partial user specializations for pointers and arrays.

When the array specialization is explicitly called, the expected value is returned.

However, when a variadic template function is used, the array parameter is converted to a pointer, and the pointer specialization is called.

Is there a way to get the compiler (g++ 4.8.1 in this case) to not do that cast?

Or is there a different way to return the "total size" that doesn't use template specializations?

#include <iostream>

template <typename T, typename...  Params>
struct TestTemplate
{
    static size_t Sizeof()
    {
        std::cout << __FILE__ << ':' << __LINE__ << std::endl;
        return sizeof (T) + TestTemplate<Params...>::Sizeof();
    }
};


template <typename T>
struct TestTemplate<T>
{
    static size_t Sizeof()
    {
        std::cout << __FILE__ << ':' << __LINE__ << std::endl;
        return sizeof (T);
    }
};


template <typename T, typename...  Params>
struct TestTemplate<T*, Params...>
{
    static size_t Sizeof()
    {
        std::cout << __FILE__ << ':' << __LINE__ << std::endl;
        return sizeof (T) + TestTemplate<Params...>::Sizeof();
    }
};


template <typename T>
struct TestTemplate<T*>
{
    static size_t Sizeof()
    {
        std::cout << __FILE__ << ':' << __LINE__ << std::endl;
        return sizeof (T);
    }
};


template <typename T, size_t N, typename...  Params>
struct TestTemplate<T[N], Params...>
{
    static size_t Sizeof()
    {
        std::cout << __FILE__ << ':' << __LINE__ << std::endl;
        return N * sizeof (T) + TestTemplate<Params...>::Sizeof();
    }
};


template <typename T, size_t N>
struct TestTemplate<T[N]>
{
    static size_t Sizeof()
    {
        std::cout << __FILE__ << ':' << __LINE__ << std::endl;
        return N * sizeof (T);
    }
};


template <typename... Params>
size_t GetSizeof (Params... params)
{
    return TestTemplate<Params...>::Sizeof();
}


struct TestType
{
    double x = 0., y = 0.;
    char buf[64];
};

int main (int, char *[])
{
    std::cout << TestTemplate<int[10]>::Sizeof() << std::endl; // prints 40. OK
    std::cout << GetSizeof (2, 3, 4) << std::endl; // prints 12. OK

    TestType tt;
    std::cout << GetSizeof (&tt, 1) << std::endl; // prints 84. OK

    int int_arr[10];
    std::cout << GetSizeof (int_arr, 1) << std::endl; // prints 8, want 41
}
1
  • Arrays decay to pointers when they're passed to a function. If you want to keep them as arrays, you must take them by reference. This has to be handled at GetSizeof() level. Commented Apr 29, 2014 at 14:41

1 Answer 1

1

You may replace your GetSizeof by: (https://ideone.com/jqXT4s)

template <typename... Params>
size_t GetSizeof (const Params&... params)
{
    return TestTemplate<Params...>::Sizeof();
}

Once you have done that, you may simply use:

template <typename T, typename...  Params>
struct TestTemplate
{
    static size_t Sizeof()
    {
        std::cout << __FILE__ << ':' << __LINE__ << std::endl;
        return sizeof (typename std::remove_pointer<T>::type) + TestTemplate<Params...>::Sizeof();
    }
};

template <typename T>
struct TestTemplate<T>
{
    static size_t Sizeof()
    {
        std::cout << __FILE__ << ':' << __LINE__ << std::endl;
        return sizeof (typename std::remove_pointer<T>::type);
    }
};

as sizeof(T[N]) == N * sizeof(T).

Sign up to request clarification or add additional context in comments.

2 Comments

now const* is tripping me up. I'm still trying different things out. https://ideone.com/uUSyaI
With const Params& instead of Params&&, it works. Answer edited.

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.