:: template除了使TMP编译之外还意味着什么 [英] What does ::template mean other than making TMP compile

查看:58
本文介绍了:: template除了使TMP编译之外还意味着什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
使用模板参数作为模板参数

这是一些高度模板化的容器类的代码片段,用于绑定任意数量的任意类型的字段.我的一位同事发现我的代码未在GCC下编译,经过大量研究,他找到了通过添加 :: template 正确地推导模板的修复程序...我们俩都不以前从未见过此内容,但除GCC不需要Visual Studio 2010的我的代码外,还不真正知道这是什么.

 模板<类型名称T,int N>struct SingleBindMemberStruct{typedef类型名称TGenericBindingHandler< T> :: BindToUse BindType;BindType m_Member;模板<类型名称ContainerClass>静态无效的AddBinding(CPackedTableDataSpec * spec){//也许使用较新版本的编译器,我们都可以找到一种都接受的语法.这是与gcc-4.5和Visual Studio 2010一起使用的#如果已定义(__GNUC__)TGenericBindingHandler< T> :: template AddBinding< ContainerClass>(spec,N,& ContainerClass :: template SingleBindMemberStruct< T,N> :: m_Member);#别的TGenericBindingHandler< T> :: template AddBinding< ContainerClass>(spec,N,& ContainerClass :: SingleBindMemberStruct< T,N> :: m_Member);#万一}}; 

有人在语法上知道可以或应该使用什么 :: template 吗?如果有人从描述它的标准中摘录了片段,那将是完美的!

听起来不错,实际上就像帮助编译器确定什么是模板一样简单,由于这是一个 static 函数,我们使用范围解析运算符而不是点运算符来告诉编译器模板.所以现在唯一剩下的问题是为什么Visual Studio也不需要它?

解决方案

它告诉编译器 AddBinding 是模板-因为 AddBinding 的定义取决于 T ,否则编译器在编译过程的正确阶段就不会知道这一点(我不是细节方面的专家,但与C ++编译模型AFAIK有关).通过在 :: 之后编写 template ,可以为编译器提供原本没有的信息.我猜更具体地说,当它在 AddBinding 之后看到< 时,它知道它是在处理模板,而不是< 运算符.

如果您想获得更详细的答案,则可能需要查看《 C ++模板:完整指南》.(如果您搜索Google,我认为它可以PDF格式提供.)

Possible Duplicate:
Using template parameters as template parameters

Here's a code snippet of some heavily templated container classes used to bind an arbitrary amount of fields of arbitrary types. A co-worker of mine found that my code didn't compile under GCC and after much research he found the fix to get it to deduce the templates correctly by adding ::template ... Neither of us had ever seen this before and still don't really know what this is other than something that GCC needs for my code that Visual Studio 2010 does not need.

    template< typename T, int N >
    struct SingleBindMemberStruct
    {
        typedef typename TGenericBindingHandler<T>::BindToUse BindType;
        BindType m_Member;

        template< typename ContainerClass >
        static void AddBinding(CPackedTableDataSpec* spec)
        {
// Perhaps with newer versions of the compilers we can find a syntax that both accept. This is with gcc-4.5 and Visual Studio 2010
#if defined(__GNUC__)
            TGenericBindingHandler<T>::template AddBinding<ContainerClass>(spec, N, &ContainerClass::template SingleBindMemberStruct<T,N>::m_Member);
#else
            TGenericBindingHandler<T>::template AddBinding<ContainerClass>(spec, N, &ContainerClass::SingleBindMemberStruct<T,N>::m_Member);
#endif
        }
    };

Does anyone know syntactically what ::template can or should be used for? If anyone has a snippet from the standard that describes it that would be perfect!

Edit:

Alright so sounds like it is really as simple as helping the compiler determine what is a template and since this is a static function we use the scope resolution operator rather than the dot operator to tell the compiler of the template. So now the only remaining question is why does Visual Studio not need this as well?

解决方案

It tells the compiler that AddBinding is a template -- since the definition of AddBinding is dependent on T, the compiler wouldn't otherwise know this at the right stage of the compilation process (I'm no expert on the details, but it's to do with the C++ compilation model AFAIK). By writing template after the ::, you give the compiler information it otherwise wouldn't have. I guess more specifically, it knows that it's dealing with a template and not a < operator when it sees < after AddBinding.

If you want a more detailed answer, you might want to check out C++ Templates: The Complete Guide. (I think it's available as a PDF if you search Google.)

这篇关于:: template除了使TMP编译之外还意味着什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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