开发者

Is this code legal in C++

I just found that when it comes to templates this code compiles in g++ 3.4.2 and works unless m() is not called:

template <typename T>
class C
{
     T e;

     public:
        C(): e(0) {};

    voi开发者_如何学God m()
    {
        e = 0;
    };
 };

Now one may create and use instance

C<const int> c;

Until c.m() is not called there are no compile errors but is this legal?


Yes, this is legal. The template specification is that until a method is instantiated, it doesn't exist and therefor is not checked by the compiler. Here's the relevant bit from the spec:

14.7.1 - Implicit instantiation

-9- An implementation shall not implicitly instantiate a function template, a member template, a non-virtual member function, a member class or a static data member of a class template that does not require instantiation.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜