C++中.template

https://rextester.com/NRAYP95711

#include <iostream>
#include <string>

static constexpr int bar() { return 22 ; }

template < typename T > auto foo_is_not_a_template()
{
    // not a template; so < and > are the relational operators (less than, greater than)
    return T::foo<34>::bar() ;
    // parsed as ( (T::foo) < 34 ) > ( ::bar() )
    // *** warning *** : these comparisons do not have their mathematical meaning
}

template < typename T > auto foo_is_a_template()
{
    // template; so < and > are beginning and end of the template argument list
   return T:: template foo<34>::bar() ;
    // parsed as: instantiate the template T::foo<34>,
    // and call the static member bar() of the instantiated class
}

struct A { static constexpr int foo = 7 ; } ;

struct B
{
    template < int N > struct foo
    {
        static std::string bar() { return std::string( N, 'k' ) ; }
    };
};

int main()
{
    std::cout << "A: foo is not a template: " << foo_is_not_a_template<A>() << '\n' ; // print integer 0
                                 // ie. ( (A::foo) < 34 ) > ::bar()
                                 // ie. (    (7)   < 34 ) >  (22)

    std::cout << "B: foo is a template: " << foo_is_a_template<B>() << '\n' ; // print string "kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk"
                            // ie. the result of call of the static member function B::foo<23>::bar()
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值