English 中文(简体)
转而行使获得收益价值或价值的职能
原标题:passing in a function to get return value or values

我有这样一个模板:

struct add_node_value_visitor : boost::static_visitor<>
{
    add_node_value_visitor(){}

    template <typename T>
    void operator() ( const T& value) const
    {
        boost::lexical_cast<std::string, T>(value);
    }

};

我面临的问题是,访问者被利用在一股价值中进行游说,由此形成的价值观的扼杀必须是一种扼杀,目前,这将产生一组不同的扼杀,这不是我想要的,以便解决这个问题。 我认为,我会给这一结构的导师增加一个职能点,以便我能够发挥职能,减轻由此造成的对每一 lo虫的扼杀和形成一种扼杀。 然后,如果我想在我不需要加固的情况下使用这一结构,我就能够这样做。 问题是,我是否应当使用一个职能点,还是有可能用诸如推动:lambda?

或者会增强:功能更容易使用?

最佳回答

与此类似?

struct add_node_value_visitor : public boost::static_visitor<>
{
public:
    typedef std::function<void(const std::string&)> concat_func_t;

    add_node_value_visitor(const concat_func_t& concat) : concat_(concat){}

    template <typename T>
    void operator() ( const T& value) const
    {
        concat_(boost::lexical_cast<std::string, T>(value));
    }

private:
    concat_func_t concat_;
};
问题回答

为什么不进行分类?

struct add_node_value_visitor : boost::static_visitor<>
{
   std::ostringstream st;
   template <typename T>
   void operator() ( const T& value ) {
      // change concatenation logic here if required (separators...)
      st << value;
   }
   std::string const & str() const {
      return st.str();
   }
};

也许会有一个更通用的来访者,让打电话者了解有关发言情况。

#include <boost/variant.hpp>
#include <boost/lexical_cast.hpp>
#include <string>
#include <vector>
#include <iostream>

struct to_string : boost::static_visitor<std::string>
{

    template <typename T>
    std::string operator() ( const T& value) const
    {
        return boost::lexical_cast<std::string, T>(value);
    }

};

int main()
{
    std::vector<boost::variant<int, double> > vec;
    vec.push_back(42);
    vec.push_back(3.14);
    std::string result;
    for (size_t i = 0; i != vec.size(); ++i) {
        result += boost::apply_visitor(to_string(), vec[i] ) +    ;
    }
    std::cout << result;
}




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签