简体   繁体   English

如何模板化外部数组?

[英]How to template an extern array?

I created a code file to keep all my global variables and one of them is an array like this one: 我创建了一个代码文件来保留所有全局变量,其中一个是这样的数组:

global.cpp global.cpp

#include <array>

array<string, 3> arr = {"value1", "value2","value3"};

I test arrays values in another code file like this: 我在另一个代码文件中测试数组值,如下所示:

testarrays.cpp testarrays.cpp

#include <iostream>
#include <array>

template <size_t N>
void TestingArrays(const array<string, N>& ArrT);

void ArrayTester()
{
     extern array<string,3> arr;

     array <string, 2> localarr = {"v1" ,"v2"};

     TestingArrays(localarr);
     TestingArrays(arr);
}

template <size_t N>
void TestingArrays(const array<string, N>& ArrT) 
{
     for (auto it = ArrT.cbegin(); it != ArrT.cend(); ++it)
     {
         cout << "Testing " << *it << endl;
     }
}

Everything is beautiful except for one thing. 除了一件事外,一切都美好。 I use this global array ( Arr ) in many other places. 我在许多其他地方都使用了此全局数组( Arr )。

That means if I need to change the number of variables (it have 3 in this example) I need to do the same over all code files... ...crazy... 这意味着如果我需要更改变量的数量(在此示例中为3),则需要对所有代码文件进行相同的操作…………疯狂……

I thought to use a template like this: 我想使用这样的模板:

testarrays.cpp testarrays.cpp

...
     template <size_t N>
     extern array<string,N> arr;
...

...but it didn't compiled. ...但是它没有编译。

Someone have a tip to solve this problem? 有人提示解决此问题吗?

A using statement could make this easier. using语句可以使此操作更容易。 Then you could use myArr in multiple places but only change the size once: 然后,您可以在多个地方使用myArr ,但只更改一次大小:

//header.h
#include <array>
#include <string>

using myArr = std::array<std::string, 3>;

extern myArr arr;

Next put the definition of the global in a new file: 接下来,将global的定义放在一个新文件中:

//myarr.cpp
#include "header.h"

myArr arr = {"value1", "value2","value3"};

Finally use it in the other compilation units (.cpp files): 最后在其他编译单元(.cpp文件)中使用它:

//main.cpp
#include "header.h"

#include <iostream>
#include <array>

template <size_t N>
void TestingArrays(const std::array<std::string, N>& ArrT);

void ArrayTester()
{
    //extern array<string, 3> arr; // global var doesn't need to be declared here if the header is included

    std::array<std::string, 2> localarr = {"v1" ,"v2"};

    TestingArrays(localarr);
    TestingArrays(arr);
}

template <size_t N>
void TestingArrays(const std::array<std::string, N>& ArrT)
{
    for(auto it = ArrT.cbegin(); it != ArrT.cend(); ++it)
    {
        std::cout << "Testing " << *it << std::endl;
    }
}

int main() {
    auto value = arr[1];
    ArrayTester();
    return 0;
}

Just make header file with the forward declaration: 只需使用前向声明制作头文件:

#pragma once
#include <array>
#include <string>

extern std::array<std::string, 3> arr;

and include it where you need: 并将其包含在您需要的地方:

#include"arr.h"
...
void ArrayTester()
{
     array <string, 2> localarr = {"v1" ,"v2"};

     TestingArrays(localarr);
     TestingArrays(arr);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM