簡體   English   中英

在C ++中將#ifdef轉換為模板元程序

[英]Turning #ifdef's into a template metaprogram in C++

我在C ++類中有以下代碼:

class Features
{
    #define Feature_Size_A 12345
    #define Feature_Size_B 45678
    #define Feature_Size_C 78901
    //#define Feature_Size_D 14725

    const int Feature_Sum = 0
    #ifdef Feature_Size_A
        + Feature_Size_A
    #endif
    #ifdef Feature_Size_B
        + Feature_Size_B
    #endif
    #ifdef Feature_Size_C
        + Feature_Size_C
    #endif
    #ifdef Feature_Size_D
        + Feature_Size_D
    #endif
        ;

    #ifdef Feature_Size_A
        static float Feature_A[Feature_Size_A];
    #endif
    #ifdef Feature_Size_B
        static float Feature_B[Feature_Size_B];
    #endif
    #ifdef Feature_Size_C
        static float Feature_C[Feature_Size_C];
    #endif
    #ifdef Feature_Size_D
        static float Feature_D[Feature_Size_D];
    #endif
};

我曾經注釋掉功能,例如第4行,以編譯和運行不同的測試。 但是現在我想將類作為模板,因此我可以在同一程序中實例化具有不同功能打開或關閉的多個版本。

我在想這樣的事情:

template <bool Feature_A, bool Feature_B, bool Feature_C, bool Feature_D>
class Features
{
    ...
};

Features<true, true, true, false> f;

我嘗試了boost :: mpl:vector,但是我在掙扎。

順便說一句:這不是完整的代碼。 原始代碼具有25個功能。

我為不涉及宏的每一個想法而感激:-)

類型列表可用於解決此問題。

template<unsigned num, unsigned size, typename T>
class Feature : public T
{
public:
    static float feature[size];
    static const unsigned int feature_sum = size + T::feature_sum;
};
template<unsigned num, unsigned size, typename T>
float Feature<num, size, T>::feature[size];
class Tail { 
public:
    static const unsigned feature_sum = 0; 
};

template<unsigned num, unsigned size, typename T>
float* get_feature_arr(Feature<num, size, T>& ref) 
{
    return ref.feature;
}

int main() {
    Feature<1, 12345, Feature<2, 45678, Feature<4, 78901, Tail>>> TripleFeatures;
    auto first = get_feature_arr<1>(TripleFeatures);
    auto third = get_feature_arr<4>(TripleFeatures);
    auto size = TripleFeatures.feature_sum;
}

這也可以用來訪問任何功能,而不管列表中是否包含其他功能。

編輯:我略述了一些細節,例如不定義數組並嘗試將“ 3features”作為標識符。 樂固定。 代碼編譯了GCC 4.7.1。

為什么不使用靜態分配的數組?

#include <stdio.h>

template <bool Feature_A, bool Feature_B, bool Feature_C, bool Feature_D>
class Features
{
    static const int Feature_Size_A = 12345;
    static const int Feature_Size_B = 45678;
    static const int Feature_Size_C = 78901;
    static const int Feature_Size_D = 14725;
    static const int Feature_Sum = 0
        + Feature_A ? Feature_Size_A : 0
        + Feature_B ? Feature_Size_B : 0
        + Feature_C ? Feature_Size_C : 0
        + Feature_D ? Feature_Size_D : 0
    ;

public:
    static float Feature_Vector_A[Feature_A ? Feature_Size_A : 0];
    static float Feature_Vector_B[Feature_B ? Feature_Size_B : 0];
    static float Feature_Vector_C[Feature_C ? Feature_Size_C : 0];
    static float Feature_Vector_D[Feature_D ? Feature_Size_D : 0];
};

Features<true, true, true, true> f1;
Features<true, true, true, false> f2;

int main()
{
    printf("%d %d\n", sizeof(f1.Feature_Vector_D), sizeof(f2.Feature_Vector_D));
}

輸出:

58900 0

目前尚不清楚這些功能的確切含義,但是下面的解決方案可以讓您有條件地包括成員函數和成員數據:

namespace mpl = boost::mpl;

// Define your features
struct FeatureA
{
    static const int size = 12345;
    static float Feature_A[size];

    static void methodA() {}
};
float FeatureA::Feature_A[12345];

struct FeatureB
{
    static const int size = 45678;
    static char Feature_B[size]; // possibly different types of data (?)

    static void methodB() {}
};
float FeatureB::Feature_B[45678];

struct FeatureC
{
    static const int size = 78901;
    static int Feature_C[size];

    static void methodC() {}
};
float FeatureC::Feature_C[78901];


// Helper metafunction
template <typename T>
struct get_size 
  : mpl::int_<T::size>
{};


template <typename FeaturesSeq>
struct Features_impl 
  : mpl::inherit_linearly<
        FeaturesSeq,
        mpl::inherit<mpl::_, mpl::_>
    >::type
{
    static const int Feature_Sum = 
        mpl::accumulate<
            FeaturesSeq,
            mpl::int_<0>,
            mpl::plus<
                mpl::_1,
                get_size<mpl::_2>
            >
        >::type::value;
};

template <typename... F>
using Features = Features_impl<mpl::vector<F...>>;


#include <iostream>

int main()
{
    typedef Features<FeatureA, FeatureC> F;

    std::cout << F::Feature_Sum << '\n';

    F::Feature_A[0] = 12.0f;
    F::methodA();

    F::methodC();
}

如果您的所有功能實際上只是浮點數組,例如您的示例,則可以使用通用Feature

template <int Size>
struct Feature
{
    static float data[Size];
};
template <int Size>
float Feature::data[Size];

並將此類的專長存儲到mpl向量中:

typedef mpl::vector<Feature<1234>, Feature<5678>> Features;

mpl::at_c<Features, 0>::type::data[0] = 12.0f;
// could be encapsulated into a helper function

沒有有關這些所謂功能目的的更多信息,很難提供更完整的答案。

暫無
暫無

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

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