Boost如何实现这种语法? [英] How is Boost able to achieve such syntax?

查看:48
本文介绍了Boost如何实现这种语法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

http://www.boost.org/doc/libs/1_58_0/doc/html/program_options/tutorial.html

// Declare the supported options.
.............................................
desc.add_options()
    ("help", "produce help message")
    ("compression", po::value<int>(), "set compression level")
;

是通过运算符重载吗?

如果是,那么哪个运算符在此过载了?

If yes, which operator was overloaded here?

您可以使用简单的非Boost示例程序来模仿此语法吗?

Can you mimic this syntax using a simple non-Boost example program?

推荐答案

希望这是不言自明的

#include <iostream>

class funky_counter
{
public:
    funky_counter() : value_(0) {}

public:
    funky_counter & increment(int value)
    {
        value_ += value;
        return *this;
    }

public:
    funky_counter & operator()(int value)
    {
        return this->increment(value);
    }

public:
    int get_value() 
    { 
        return value_; 
    }

private:
    int value_;
};

int main(void)
{
    funky_counter counter;

    counter.increment(2) (5) (7);

    std::cout <<  counter.get_value() << std::endl;
}

这篇关于Boost如何实现这种语法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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