先上代码
example 1:
template <typename T>
class Blocker : {public:using MessageType = T;using MessageShrPtr = std::shared_ptr<T>;using MessageQueue = std::deque<MessageShrPtr>;using Callback = std::function<void(const MessageShrPtr)>;using CallbackMap = std::unordered_map<std::string, Callback>;using Iterator = typename std::deque<std::shared_ptr<T>>::const_iterator;explicit Blocker(const BlockerAttr& attr);virtual ~Blocker();void Publish(const MessageType& msg);void Publish(const MessageShrPtr msg);bool Subscribe(const std::string& callback_id, const Callback& callback);Iterator ObservedBegin() const;Iterator ObservedEnd() const;private:MessageQueue observed_msg_queue_;CallbackMap published_callbacks_;
};
example 2:
using smart::cyber::common::GlobalData;std::unique_ptr<Node> CreateNode(const std::string& node_name,const std::string& name_space) {bool is_reality_mode = GlobalData::Instance()->IsRealityMode();std::unique_ptr<Node> node(new Node(node_name, name_space));return std::move(node);
}
using 主要功能:
1 别名
主要目的是简化代码;解决人类懒得问题,懒也是驱动力。
类型别名
using CallbackMap = std::unordered_map<std::string, Callback>;
using Iterator = typename std::deque<std::shared_ptr<T>>::const_iterator;
模板别名
using MessageType = T;
命名空间别名
namespace user_xx_yy_dd {int func() {return 5;}
}
using udd = user_xx_yy_dd;
int value = udd::func();
2. 导入命名空间
使用using关键字可以将命名空间中的单个成员或整个命名空间导入到当前作用域,减少代码的冗余。
using smart::cyber::common::GlobalData;
using std::string;
导入整个命名空间(少用,这种方式很粗狂)
using namespace std;