1

boost::mpl::eval_if空でないテンプレート引数の数を与えることができる(または同様の関数)の結果に応じて、いくつかの引数を使用してマクロを呼び出したいと思います。次のような擬似コードがあるとします。

struct EmptyType {  };
template<class arg1=EmptyType, class arg2=EmptyType, class arg3=EmptyType>
class my_class
{
     eval_if<is_not_same<arg1, EmptyType>, FILL_MY_CLASS_DEFINE(1)> else      
     eval_if<is_not_same<arg2, EmptyType>, FILL_MY_CLASS_DEFINE(2)> else
     eval_if<is_not_same<arg3, EmptyType>, FILL_MY_CLASS_DEFINE(3)>;
};

引数の数に応じて、クラスをコンテンツで埋めようとしていますEmptyType。Boost.MPL/プリプロセッサまたは他のBoostライブラリを介してC++03でそのようなことをどのように行うことができるのだろうか?

4

3 に答える 3

2

プリプロセッサやmplは必要ありません。部分的な特殊化が必要です:

編集これはC++03で機能します。ライブで確認してください:https ://ideone.com/6MaHJ

#include <iostream>
#include <string>

struct EmptyType {  };

template<class  arg1=EmptyType, class arg2=EmptyType, class arg3=EmptyType>
class my_class
{
    // FILL_MY_CLASS_DEFINE(3)
};
template<class  arg1, class arg2>
class my_class<arg1,arg2,EmptyType>
{
    // FILL_MY_CLASS_DEFINE(2)
};
template<class  arg1>
class my_class<arg1,EmptyType,EmptyType>
{
    // FILL_MY_CLASS_DEFINE(1)
};
template<>
class my_class<EmptyType,EmptyType,EmptyType>
{
    // FILL_MY_CLASS_DEFINE(0)
};

int main(int argc, const char *argv[])
{
    my_class<std::string, double, int> a;
    my_class<std::string, int> b;
    my_class<void> c;

    return 0;
}
于 2011-12-09T08:09:21.340 に答える
1

可変個引数テンプレートをお探しですか?

#include <tuple>
#include <iostream>
#include <string>

template <typename... Arg>
struct my_class
{
    // getting the size of the argument list:
    enum { size = sizeof...(Arg) }; // in absense of static fields with initializers...

    // demo filling the struct with data:
    std::tuple<Arg...> arg_data;
    my_class(Arg&&... a) : arg_data(std::forward<Arg>(a)...) { }
};

int main(int argc, const char *argv[])
{
    my_class<std::string, int> a("hello world", 42);

    std::cout << "size: " << a.size << std::endl;
    std::cout << "last: " << std::get<a.size-1>(a.arg_data) << std::endl;

    return 0;
}

出力:

size: 2
last: 42
于 2011-12-09T08:50:15.380 に答える
0

多くのテンプレート引数がある場合、部分的な特殊化は非現実的でエラーが発生しやすい可能性があります。以下のコードはあなたが望むことを実行しますが、他の回答ですでに述べられているように、それが常に最善の方法であるとは限りません。

#include <boost/mpl/count_if.hpp>
#include <boost/mpl/not.hpp>
#include <boost/type_traits/is_same.hpp>

using boost::is_same;
using boost::mpl::_;
using boost::mpl::not_;
using boost::mpl::count_if;

#define FILL_MY_CLASS_DEFINE(x) static const int __x__ = x // an example, watch out: no semicolon at the end

struct EmptyType {  };
template<class arg1=EmptyType, class arg2=EmptyType, class arg3=EmptyType>
class my_class
{
    // count the types which are not equal to EmptyType
    static const long NonEmptyCount = count_if<type, not_<is_same<_, EmptyType> > >::value;
    // invoke a macro with an argument
    FILL_MY_CLASS_DEFINE(NonEmptyCount);
};
于 2012-01-16T15:52:39.890 に答える