c++模板中的百分号(%)和尖号(#)是什么意思?

what is the meaning of percent symbol(%) and sharp symbol(#) in c++ template

本文关键字:是什么 意思 百分 c++      更新时间:2023-10-16

下面是来自MS VC的代码:

 template<typename _Fun_t,
    typename _Arg_t> inline
    binder1st<_Fun_t> bind1st(_Fun_t% _Func, _Arg_t _Left)
    {   // return a binder1st functor adapter
    typename _Fun_t::first_argument_type _Val = _Left;
    return (binder1st<_Fun_t>(_Func, _Val));
    }

和QT:

 #define Q_ARG(type, data) QArgument<type >(#type, data)

这些都不是模板所特有的。

'%'是c++的Microsoft扩展,是c++/CLI的一部分。它定义了一个跟踪引用。T&类型的正常左值引用变量是对另一个变量的引用;T%也是如此,除了它指向一个可能被垃圾回收器移动的托管对象;GC知道,当它移动对象时,它必须修补对该对象的所有跟踪引用。

'#'是C预处理器的字符串化操作符。它表示下面的宏参数的值,用双引号括起来。所以这:

Q_ARG(MyType, 12345)

将展开为:

QArgument<MyType >("MyType", 12345);