C++ 中非成员函数的广义链接;

发布于 2024-12-06 10:22:52 字数 1021 浏览 0 评论 0原文

我不知道这是否可以实现,但是考虑到这些函数\类集:

float plus1(float x) { return x+1; }
float div2(float x) { return x/2.0f; }
template <typename T>
class chain {
public:
    chain(const T& val = T()) : val_(val) {}
    chain& operator<<( std::function<float (float)> func ) {
    val_ = func(val_);
    return *this;
  }
  operator T() const {
    return val_;
  }
  T val_;
};

我可以像这样链接在浮点数上运行的函数:

float x = chain<float>(3.0f) << div2 << plus1 << div2 << plus1;

但是,我想将其概括\扩展为能够在类型和类型之间进行转换具有带参数的函数。不幸的是,我不够聪明,无法弄清楚如何或是否可以做到这一点。 更具体地说,我希望能够做这样的事情(其中 operator<< 只是一个任意选择,最好我什至不必编写“链”开头); 另外,这些只是虚拟示例,我不打算将其用于算术。

std::string str = chain<float>(3.0) << mul(2.0f) << sqrt << to_string << to_upper;

或者

vec3d v = chain<vec3i>(vec3i(1,1,1)) << normalize << to_vec3<double>;

有什么想法吗?

I don't know if this can even be achivieable, but given these set of functions\class:

float plus1(float x) { return x+1; }
float div2(float x) { return x/2.0f; }
template <typename T>
class chain {
public:
    chain(const T& val = T()) : val_(val) {}
    chain& operator<<( std::function<float (float)> func ) {
    val_ = func(val_);
    return *this;
  }
  operator T() const {
    return val_;
  }
  T val_;
};

I can chain functions operating on floats like this:

float x = chain<float>(3.0f) << div2 << plus1 << div2 << plus1;

However, I'd like to generalize\extend this to being able to convert between types and have functions with arguments. Unfortunately I'm not smart enough to figure out how, or if, this can be done.
Too be more specific I'd like to be able to do something like this (Where operator<< is just an arbitary choice, and preferably I dont even have to write "chain" at the beginning);
Also, these are just dummy examples, I do not intend to use it for arithmetics.

std::string str = chain<float>(3.0) << mul(2.0f) << sqrt << to_string << to_upper;

or

vec3d v = chain<vec3i>(vec3i(1,1,1)) << normalize << to_vec3<double>;

Any ideas?

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

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

发布评论

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

评论(4

若能看破又如何 2024-12-13 10:22:52

我想我明白你为什么想这么做了。它类似于 iostream 操纵器。

您始终需要从 chain(...) 开始(即您永远无法神奇地执行 int x = 1 << plus(2) << ; times(2)),但您可以重载运算符 int运算符 float,...以允许隐式转换。

您还需要返回并定义每种类型(例如 mul),然后实现operator<<,它接受 mul 或 const mul,但作为一个整体这是可行的(但 PITA)

I think i see why you want to do it. It's similar to the iostream manipulators.

You will always need to start with chain(...) (i.e you will never be able to magically do something like int x = 1 << plus(2) << times(2)), but you can overload the operator int, operator float, ... to allow for the implicit conversions.

You will also need to go back and define each type (like mul) and then implement the operator<< which takes a mul or a const mul, but as a whole it's doable (but a PITA)

云仙小弟 2024-12-13 10:22:52

使用 boost::proto 的通用且可扩展的解决方案:

#include <iostream>
#include <boost/proto/proto.hpp>

namespace bp = boost::proto;

// -----------------------------------------------------------------------------
// perform is a callable transform that take a function_ terminal and execute it
// -----------------------------------------------------------------------------
struct perform : bp::callable
{
  template<class Sig> struct result;
  template<class This, class Func, class In>
  struct result<This(Func,In)> 
       : boost::result_of<typename boost::remove_reference<Func>::type(In)> {};

  template<class Func, class In>
  typename result<perform(Func &,In)>::type
  operator()( Func& f, In& in ) const
  {
    return f(in);
  }
};

// -----------------------------------------------------------------------------
// Grammar for chaining pipe of functions
// -----------------------------------------------------------------------------
struct pipeline_grammar
: bp::or_<
    bp::when<
        bp::bitwise_or<pipeline_grammar,pipeline_grammar>
          , pipeline_grammar(
                bp::_right
              , pipeline_grammar(bp::_left,bp::_state)
                )
        >
      , bp::when<
            bp::terminal<bp::_>
          , perform(bp::_value, bp::_state) 
    >
> {};

// -----------------------------------------------------------------------------
// Forward declaration of the pipeline domain
// -----------------------------------------------------------------------------
struct pipeline_domain;

// -----------------------------------------------------------------------------
// A pipeline is the top level DS entity
// -----------------------------------------------------------------------------
template<class Expr>
struct  pipeline : bp::extends<Expr,pipeline<Expr>, pipeline_domain>
{
  typedef bp::extends<Expr, pipeline<Expr>, pipeline_domain> base_type;
  pipeline(Expr const &expr = Expr()) : base_type(expr) {}

  // ---------------------------------------------------------------------------
  // A pipeline is an unary callable object
  // ---------------------------------------------------------------------------
  template<class Input>
  typename boost::result_of<pipeline_grammar(pipeline,Input)>::type
  operator()(Input const& in) const
  {
    pipeline_grammar evaluator;
    return evaluator(*this,in);
  }
};

// -----------------------------------------------------------------------------
// the pipeline_domain make pipeline expression macthes pipeline_grammar
// -----------------------------------------------------------------------------
struct pipeline_domain 
     : bp::domain<bp::generator<pipeline>,pipeline_grammar>
{};

// -----------------------------------------------------------------------------
// Takes a PFO instance and make it a pipeline terminal
// -----------------------------------------------------------------------------
template<class Func>
typename bp::result_of::
make_expr<bp::tag::terminal, pipeline_domain,Func>::type
task( Func const& f )
{
  return bp::make_expr<bp::tag::terminal,pipeline_domain>( f );
}

//--------------------------- Examples --------------------

struct return_value
{  
  template<class Sig> struct result;
  template<class This, class T>
  struct result<This(T)> : bp::detail::uncvref<T>
  {};

  return_value(int i = 1) : factor(i) {}

  template<class T> 
  T operator()(T const& in) const
  {
    return in*factor;
  }

  int factor;
};

struct say_hi
{
  typedef void result_type;

  template<class T> 
  void operator()(T const& in) const
  {
    std::cout << "Hi from value = " << in << "\n";
  }
};

int main()
{
  return_value r1,r2(5);
  (task(r1) | task(r2) | task(say_hi())) (7); // SHould print 35

  float k = 10,r;
  r = (task(r2) | task(r2) | task(r2) | task(r2))(k);
  std::cout << r << "\n"; // Should print 6250
}

基本思想是将函数对象包装为 proto 终端,构建一个小型 |基于语法并让原型系统处理组合。

A general and extendable solution using boost::proto :

#include <iostream>
#include <boost/proto/proto.hpp>

namespace bp = boost::proto;

// -----------------------------------------------------------------------------
// perform is a callable transform that take a function_ terminal and execute it
// -----------------------------------------------------------------------------
struct perform : bp::callable
{
  template<class Sig> struct result;
  template<class This, class Func, class In>
  struct result<This(Func,In)> 
       : boost::result_of<typename boost::remove_reference<Func>::type(In)> {};

  template<class Func, class In>
  typename result<perform(Func &,In)>::type
  operator()( Func& f, In& in ) const
  {
    return f(in);
  }
};

// -----------------------------------------------------------------------------
// Grammar for chaining pipe of functions
// -----------------------------------------------------------------------------
struct pipeline_grammar
: bp::or_<
    bp::when<
        bp::bitwise_or<pipeline_grammar,pipeline_grammar>
          , pipeline_grammar(
                bp::_right
              , pipeline_grammar(bp::_left,bp::_state)
                )
        >
      , bp::when<
            bp::terminal<bp::_>
          , perform(bp::_value, bp::_state) 
    >
> {};

// -----------------------------------------------------------------------------
// Forward declaration of the pipeline domain
// -----------------------------------------------------------------------------
struct pipeline_domain;

// -----------------------------------------------------------------------------
// A pipeline is the top level DS entity
// -----------------------------------------------------------------------------
template<class Expr>
struct  pipeline : bp::extends<Expr,pipeline<Expr>, pipeline_domain>
{
  typedef bp::extends<Expr, pipeline<Expr>, pipeline_domain> base_type;
  pipeline(Expr const &expr = Expr()) : base_type(expr) {}

  // ---------------------------------------------------------------------------
  // A pipeline is an unary callable object
  // ---------------------------------------------------------------------------
  template<class Input>
  typename boost::result_of<pipeline_grammar(pipeline,Input)>::type
  operator()(Input const& in) const
  {
    pipeline_grammar evaluator;
    return evaluator(*this,in);
  }
};

// -----------------------------------------------------------------------------
// the pipeline_domain make pipeline expression macthes pipeline_grammar
// -----------------------------------------------------------------------------
struct pipeline_domain 
     : bp::domain<bp::generator<pipeline>,pipeline_grammar>
{};

// -----------------------------------------------------------------------------
// Takes a PFO instance and make it a pipeline terminal
// -----------------------------------------------------------------------------
template<class Func>
typename bp::result_of::
make_expr<bp::tag::terminal, pipeline_domain,Func>::type
task( Func const& f )
{
  return bp::make_expr<bp::tag::terminal,pipeline_domain>( f );
}

//--------------------------- Examples --------------------

struct return_value
{  
  template<class Sig> struct result;
  template<class This, class T>
  struct result<This(T)> : bp::detail::uncvref<T>
  {};

  return_value(int i = 1) : factor(i) {}

  template<class T> 
  T operator()(T const& in) const
  {
    return in*factor;
  }

  int factor;
};

struct say_hi
{
  typedef void result_type;

  template<class T> 
  void operator()(T const& in) const
  {
    std::cout << "Hi from value = " << in << "\n";
  }
};

int main()
{
  return_value r1,r2(5);
  (task(r1) | task(r2) | task(say_hi())) (7); // SHould print 35

  float k = 10,r;
  r = (task(r2) | task(r2) | task(r2) | task(r2))(k);
  std::cout << r << "\n"; // Should print 6250
}

The basic idea is to wrap function objects as proto terminals, build a small | based grammar and let the proto system deals with the composition.

愿得七秒忆 2024-12-13 10:22:52

为了获得类型之间的转换,您希望所有内容都返回一个可以转换为任何类型的代理对象。也许是基于 boost::variant 的东西。

您还可以重写您的运算符 <<作为模板函数,使其更加通用:

template <class UnaryFunction>
chain& operator<<(UnaryFunction func) { _val = func(_val); return *this;}

这将允许您使用任何类型的函数对象作为参数。

要使用具有多个参数的函数,可以使用绑定函数。这是在 C++11 之前的 boost 中,但现在它已在标准中,并且应该在任何 C++11 兼容编译器上可用。

In order to get conversions between types you would want to have everything return a proxy object, that could convert to any type. Something based on boost::variant, perhaps.

You could also rewrite your operator << as a template function to make it a bit more generic:

template <class UnaryFunction>
chain& operator<<(UnaryFunction func) { _val = func(_val); return *this;}

That would allow you to use any kind of function object as an argument.

To use functions with multiple arguments, you can use the bind function. This was in boost prior to C++11, however now it is in the standard and should be available on any C++11 compatible compiler.

戏剧牡丹亭 2024-12-13 10:22:52

这是我的 C++17 解决方案。

#include <type_traits>
#include <utility>

template <class F>
struct waterfall
{
    waterfall(F&& f)
    : fn(std::forward<F>(f))
    {}

    template <class... Args>
    decltype(auto) operator()(Args&&... args) const {
        return fn(std::forward<Args>(args)...);
    }

    template <class T>
    auto then(T&& t) const & {
        return then_impl(fn, std::forward<T>(t));
    }

    template <class T>
    auto then(T&& t) const && {
        return then_impl(std::move(fn), std::forward<T>(t));
    }

private:
    F fn;

    template <class In, class Out>
    static auto then_impl(In&& in, Out&& out)
    {
        auto fn = [in = std::forward<In>(in), out = std::forward<Out>(out)](auto&&... args)
        {
            using InRet = std::invoke_result_t<In, decltype(args)...>;

            if constexpr (std::is_invocable_v<Out, InRet>) {
                return out(in(std::forward<decltype(args)>(args)...));
            }
            else {
                in(std::forward<decltype(args)>(args)...);
                return out();
            }
        };

        return waterfall<decltype(fn)>(std::move(fn));
    }
};

像这样使用它

int main()
{
    // Create a chain
    waterfall chain([](const char* s) {
        return 42;
    })
    .then([](auto x) {
        // x = 42 here
        return x + 1;
    })
    .then([] {
        // Ignoring value from previous function.
        // Send double to next one.
        return 3.14;
    })
    .then([](double value) {
        // etc...
        return true;
    });

    // chain signature is now bool(const char*)

    // Now call our functions in chain
    bool ret = chain("test");
}

Here is my solution for C++17.

#include <type_traits>
#include <utility>

template <class F>
struct waterfall
{
    waterfall(F&& f)
    : fn(std::forward<F>(f))
    {}

    template <class... Args>
    decltype(auto) operator()(Args&&... args) const {
        return fn(std::forward<Args>(args)...);
    }

    template <class T>
    auto then(T&& t) const & {
        return then_impl(fn, std::forward<T>(t));
    }

    template <class T>
    auto then(T&& t) const && {
        return then_impl(std::move(fn), std::forward<T>(t));
    }

private:
    F fn;

    template <class In, class Out>
    static auto then_impl(In&& in, Out&& out)
    {
        auto fn = [in = std::forward<In>(in), out = std::forward<Out>(out)](auto&&... args)
        {
            using InRet = std::invoke_result_t<In, decltype(args)...>;

            if constexpr (std::is_invocable_v<Out, InRet>) {
                return out(in(std::forward<decltype(args)>(args)...));
            }
            else {
                in(std::forward<decltype(args)>(args)...);
                return out();
            }
        };

        return waterfall<decltype(fn)>(std::move(fn));
    }
};

And use it like this

int main()
{
    // Create a chain
    waterfall chain([](const char* s) {
        return 42;
    })
    .then([](auto x) {
        // x = 42 here
        return x + 1;
    })
    .then([] {
        // Ignoring value from previous function.
        // Send double to next one.
        return 3.14;
    })
    .then([](double value) {
        // etc...
        return true;
    });

    // chain signature is now bool(const char*)

    // Now call our functions in chain
    bool ret = chain("test");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文