C ++元编程 [英] C++ Meta-programming

查看:187
本文介绍了C ++元编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C ++ 开发我的项目时,我经常需要调试,我通常使用这个宏来做。

  #define DBUG(a){std :: cout< #a<< :<< a<< std :: endl;}; 

但很多时候我需要做这样的事情

  int a; 
std :: string b;
double c;
...
...
DBG(a); DBG(b); DBG(c);但是理想情况下,可能只需写 DBUG(a,b,b),就可以写成

c) DBG(a,b,c,d,e),以获得更多变量。经过一些研究,这看起来像一个问题在元编程或更具体的代码生成,但由于我在这些领域的有限的知识,我找不到一个办法。



如果可能,我想解决这个问题,而不使用Boost或其他外部库,并使用 C ++ 98 中的功能,虽然如果不可能, m愿意使用 C + + 11

解决方案

t类似于对特定数量的参数的限制。我没有找到一个好的方法,静态解码名称,所以名称放在一起作为逗号分隔的字符串,然后在运行时解码。总的来说,这可能有点太重了,但至少,它是如所问,并没有限制参数的数量(除了编译器的限制,即):

  #include< iostream> 
#include< sstream>
#include< iterator>
#include< algorithm>
#include< tuple>
#include< vector>
#include< type_traits>
#include< stdlib.h>

template< int I,int S,typename ... V>
typename std :: enable_if< I == S> :: type
debug_var(std :: vector< std :: string> const& std :: tuple< V ...& )
{
}

template< int I,int S,typename ... V>
typename std :: enable_if< I!= S> :: type
debug_var(std :: vector< std :: string> const& n,std :: tuple< V ...& ; v)
{
std :: cout<< n [I] '='<< std :: get< I>(v)< '';
debug_var< I + 1,S>(n,v);
}

template< typename ... V>
void debug(std :: vector< std :: string> const& n,std :: tuple< V ...> const& v)
{
debug_var< 0,sizeof ...(V)>(n,v);
std :: cout<< '\\\
'<< std :: flush;
}

std :: vector< std :: string> debug_names(char const * names)
{
std :: vector< std :: string>结果;
std :: istringstream((names));
for(std :: string name; std :: getline(in>> std :: ws,name,',');){
result.push_back
}
return result;
}

#define DEBUG(...)debug(debug_names(#__ VA_ARGS__),std :: tie(__ VA_ARGS__));

int main()
{
int a = 1,b = 2;
DEBUG(a,b);
DEBUG();
}

该代码使用了2011年C ++版本引入的几个功能。 / p>

During development of my project in C++ I have a frequent need of debugging and I usually use this macro to do it

#define DBUG(a) {std::cout << #a << " : " << a << std::endl;};

But many times I need to do something like this

int a;
std :: string b;
double c;
...
...
DBG(a); DBG(b); DBG(c);

But ideally it might be possible to just write DBUG(a, b, c) or DBG(a, b, c, d, e) for more variables to achieve something like this. After some research this looked like a problem in meta-programming or more specifically code-generation, but because of my limited knowledge in these areas I could not find a way to go about it.

If possible I would like to solve this without using Boost or other external libraries, and using the features in C++98 although if it is not possible I'm willing to use C++11.

解决方案

I don't like a limitation to a specific number of arguments. I didn't find a nice approach which decodes the name statically so the names are put together as a comma separated string and then decoded at run-time. Overall, that may be a bit too heavy-weight but, at least, it does as was asked and has not limitation on the number of arguments (other than compiler limitations, that is):

#include <iostream>
#include <sstream>
#include <iterator>
#include <algorithm>
#include <tuple>
#include <vector>
#include <type_traits>
#include <stdlib.h>

template <int I, int S, typename... V>
typename std::enable_if<I == S>::type
debug_var(std::vector<std::string> const&, std::tuple<V...> const&)
{
}

template <int I, int S, typename... V>
typename std::enable_if<I != S>::type
debug_var(std::vector<std::string> const& n, std::tuple<V...> const& v)
{
    std::cout << n[I] << '=' << std::get<I>(v) << ' ';
    debug_var<I + 1, S>(n, v);
}

template <typename... V>
void debug(std::vector<std::string> const& n, std::tuple<V...> const& v)
{
    debug_var<0, sizeof...(V)>(n, v);
    std::cout << '\n' << std::flush;
}

std::vector<std::string> debug_names(char const* names)
{
    std::vector<std::string> result;
    std::istringstream in(names);
    for (std::string name; std::getline(in >> std::ws, name, ','); ) {
        result.push_back(name);
    }
    return result;
}

#define DEBUG(...) debug(debug_names(#__VA_ARGS__), std::tie(__VA_ARGS__));

int main()
{
    int a=1, b=2;
    DEBUG(a, b);
    DEBUG();
}

The code uses several features which were introduced by the 2011 revision of C++.

这篇关于C ++元编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆