簡體   English   中英

在C++初始化一個模板class私有static變量

[英]Initialize a template class private static variable in C++

我正在嘗試使用模板在 C++ 中編譯示例程序。 模板 class 有一個 priavte static 成員變量,在嘗試編譯時似乎未定義。 通過 SO 上的其他答案,我意識到這個變量也需要定義。 然而,到目前為止,我嘗試定義這個變量都沒有成功,這可能是因為我缺乏使用模板的經驗。 這是我的示例程序:

#include <iostream>
#include <array>

enum FRUIT
{
    APPLE,
    ORANGE
};

using FunctionPtr = void(*)(void);

template <FRUIT T>
void FruitFunction(void);

template <FRUIT...TotalFruits>
class TestClass
{
public:

    struct fruitGroup
    {
        FRUIT fruit;
        FunctionPtr func;
    };

    static int find_fruit(FRUIT fruit, int arg)
    {
        for (auto i = pv_mem_.begin(); i != pv_mem_.end(); ++i) {
            if (i->fruit == fruit) {
                break;
            }
        }

        return 0;
    }

private:
    constexpr static std::array<fruitGroup, sizeof...(TotalFruits)> pv_mem_ 
    {
        fruitGroup{TotalFruits, &FruitFunction<TotalFruits>}...
    };
};

int main()
{
    TestClass<FRUIT::APPLE, FRUIT::ORANGE> test;
    test.find_fruit(FRUIT::APPLE, 0);

    return 0;
}

這產生:

$ g++ -std=c++11 fruit.cpp -o foo
/tmp/ccqaSBYm.o: In function `TestClass<(FRUIT)0, (FRUIT)1>::find_fruit(FRUIT, int)':
fruit.cpp:(.text._ZN9TestClassIJL5FRUIT0ELS0_1EEE10find_fruitES0_i[_ZN9TestClassIJL5FRUIT0ELS0_1EEE10find_fruitES0_i]+0xf): undefined reference to `TestClass<(FRUIT)0, (FRUIT)1>::pv_mem_'
fruit.cpp:(.text._ZN9TestClassIJL5FRUIT0ELS0_1EEE10find_fruitES0_i[_ZN9TestClassIJL5FRUIT0ELS0_1EEE10find_fruitES0_i]+0x1d): undefined reference to `TestClass<(FRUIT)0, (FRUIT)1>::pv_mem_'
collect2: error: ld returned 1 exit status

我嘗試將pv_mem_定義為:

constexpr static std::array<TestClass::fruitGroup, sizeof...(TotalFruits)> pv_mem_;

但這導致了以下錯誤:

$ g++ -std=c++11 fruit.cpp -o foo
fruit.cpp:44:74: error: wrong number of template arguments (1, should be 2)
 constexpr static std::array<TestClass::fruitGroup, sizeof...(TotalFruits)> pv_mem_;
                                                                          ^
In file included from fruit.cpp:2:0:
/usr/include/c++/5/array:89:12: note: provided for ‘template<class _Tp, long unsigned int _Nm> struct std::array’
     struct array
            ^
fruit.cpp:44:76: error: uninitialized const ‘pv_mem_’ [-fpermissive]
 constexpr static std::array<TestClass::fruitGroup, sizeof...(TotalFruits)> pv_mem_;
                                                                            ^

初始化此變量的正確方法是什么?

pv_mem_定義如下

constexpr static std::array<fruitGroup, sizeof...(TotalFruits)> pv_mem_ 
{
  fruitGroup{TotalFruits, &FruitFunction<TotalFruits>}...
};

它使用&來獲取FruitFunction<TotalFruits>的地址,但是由於FruitFunction只是聲明而沒有定義,它會在運行時產生一個未定義的引用錯誤。

添加模板 function FruitFunction的定義將解決 C++17 中的問題

template <FRUIT T>
void FruitFunction() { /* */ }

C++11中constexpr static成員變量還需要定義在class之外,所以還需要加上

template <FRUIT...TotalFruits>
constexpr std::array<
  typename TestClass<TotalFruits...>::fruitGroup, 
  sizeof...(TotalFruits)> TestClass<TotalFruits...>::pv_mem_;

演示

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM