You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何正确使用using-declarations?解决cpplint与编译报错矛盾

关于std::placeholders的using声明写法问题

问题描述

最初使用的代码:

using namespace std::placeholders;
my_cb = std::bind(&Myclass::handler, this, _1, _2);

代码运行正常,但cpplint提示应使用using-declarations而非using-directives。

修改后的代码:

using _1 = std::placeholders::_1;
using _2 = std::placeholders::_2;
my_cb = std::bind(&Myclass::handler, this, _1, _2);

此时编译器报错:

error: ‘_1’ in namespace ‘std::placeholders’ does not name a type

请问正确的using-declarations写法是什么?

更新说明

目前收到的回复建议恢复using namespace std::placeholders;语句,但cpplint对此并不认可,提示信息为:

Do not use namespace using-directives. Use using-declarations instead.

请问该如何解决这个问题?

解决方案

std::placeholders::_1、_2这类占位符并不是类型,而是命名空间下的对象实例,因此不能用类型别名的语法声明。正确的using声明写法是直接引入这些对象的名字:

using std::placeholders::_1;
using std::placeholders::_2;

my_cb = std::bind(&Myclass::handler, this, _1, _2);

这种写法既符合cpplint对using-declarations的要求,也能让编译器正确识别占位符,不会触发报错。

内容的提问来源于stack exchange,提问作者my_question

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 08:56:07