使用 boost::mpl,我如何获得有多少模板类不是“空”,并用这个数字调用一些宏?

发布于 2024-12-20 07:30:45 字数 607 浏览 9 评论 0原文

我想根据 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/Preprocessor 或其他 Boost 库在 C++03 中完成这样的事情?

I want to call a macro with some arguments depending on the result of boost::mpl::eval_if (or a similar function) that could give how many template arguments are not empty. Say we have some pseudocode like the following:

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)>;
};

I am trying to fill my class with some content depending on how many arguments are EmptyType. I wonder how such thing can be done in C++03 via Boost.MPL/Preprocessor or some other Boost library?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蓝眼泪 2024-12-27 07:30:45

您不需要预处理器或 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;
}

You don't need preprocessor or mpl. Partial specialization is you need:

Edit This works in C++03, see it live: 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;
}
百变从容 2024-12-27 07:30:45

您在寻找可变参数模板吗?

#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

Are you looking for variadic templates?

#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;
}

Output:

size: 2
last: 42
情归归情 2024-12-27 07:30:45

当您有许多模板参数时,部分特化可能不切实际并且容易出错。
下面的代码将执行您想要的操作,但正如其他答案中已经提到的那样,它并不总是最好的继续方式。

#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);
};

When you have many template arguments, a partial specialization can be impractical and error-prone.
The code below will do what you want, but as it was already mentioned in other answers, it's not always the best way to proceed.

#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);
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文