Template Parameter with implicit array size

孤街醉人 提交于 2019-12-14 03:54:02

问题


Below is a simplified template class that accept an array as a template parameter. However I have to pass also the size of the array as a parameter. I would like to deduce it automatically and to write just:

const char *TextArray[] = { "zero", "one", "two" };

Array<TextArray> a;

In the actual implementation the class knows the size of TextArray at compile time, this is required (because at compiler time it is checked and paired with other items in the class). I get correctly a compiler error if I specify the wrong size:

Array<100, TextArray> a;

Class definiton:

#include <iostream>

template <std::size_t N, const char * (&A)[N]>
class Array
{
public:
    auto getCount()
    {
        return N;
    }
    auto getAt(std::size_t n)
    {
        return A[n];
    }
};


const char *TextArray[] = { "zero", "one", "two" };

int main() {
    Array<sizeof(TextArray)/sizeof(TextArray[0]), TextArray> a;

    printf("a.getCount() is %zu\n", a.getCount());
    printf("a.getAt(1) is %s\n", a.getAt(1));
}

Output:

a.getCount() is 3

a.getAt(1) is one

A solution is to use a Macro, but I don't want to pollute the global scope. A trivial improvement is to update the class so that I write:

Array<sizeof(TextArray), TextArray> a;

Using C++17 on gcc, Visual Studio, clang


回答1:


You can use auto in template parameter since C++17, e.g.

template <auto &A>
class Array
{
public:
    auto getCount()
    {
        return std::size(A); // also C++17 feature, #include <iterator>
    }
    auto getAt(std::size_t n)
    {
        return A[n];
    }
};

BTW, you'd better explicitly cast a.getCount() to unsigned to match the %u specifier.



来源:https://stackoverflow.com/questions/48241440/template-parameter-with-implicit-array-size

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