[cfe-dev] On instantiations of template declarations.

Abramo Bagnara abramo.bagnara at gmail.com
Fri Jun 4 02:37:42 PDT 2010


Il 04/06/2010 09:34, John McCall ha scritto:
> 
> On Jun 3, 2010, at 11:28 PM, Abramo Bagnara wrote:
> 
>> Il 01/06/2010 18:41, Abramo Bagnara ha scritto:
>>> Il 01/06/2010 17:44, Douglas Gregor ha scritto:
>>>>
>>>> On Jun 1, 2010, at 7:38 AM, Enea Zaffanella wrote:
>>>>> Consider now the following variant, where we have a static data member:
>>>>>
>>>>> template <typename T>
>>>>> struct A { static int a; };
>>>>>
>>>>> template <typename T>
>>>>> int A<T>::a = 0;
>>>>>
>>>>> A<int> si;
>>>>> A<double> sd;
>>>>>
>>>>> void bar() {
>>>>>  sd.a = 13;
>>>>> }
>>>>>
>>>>> Here, we have two implicit instantiations of the template A.
>>>>> Each of those declares the corresponding static data member.
>>>>> For one of those (A<double>), we also have an implicit instantiation of 
>>>>> the *definition* of the static data member A<double>::a.
>>>>> This implicit definition is reachable starting from the implicit 
>>>>> declaration using method:
>>>>>
>>>>>  VarDecl* VarDecl::getOutOfLineDefinition();
>>>>>  If this is a static data member, find its out-of-line definition.
>>>>>
>>>>> Strangely, the very same implicit definition is also listed in the 
>>>>> context of the translation unit. This can be seen when dumping the AST, 
>>>>> just after the definition of function bar():
>>>>>
>>>>> ===================
>>>>> [...]
>>>>> void bar() (CompoundStmt 0x2151fb0 <var_template.cc:10:12, line:12:1>
>>>>>  (BinaryOperator 0x216be70 <line:11:3, col:10> 'int' '='
>>>>>    (MemberExpr 0x216bdf0 <col:3, col:6> 'int' .a 0x216b160
>>>>>      (DeclRefExpr 0x216bdb0 <col:3> 'A<double>':'struct A<double>' 
>>>>> Var='sd' 0x216ae10))
>>>>>    (IntegerLiteral 0x216be30 <col:10> 'int' 13)))
>>>>>
>>>>>
>>>>> static int a = (IntegerLiteral 0x216a8d0 <var_template.cc:5:15> 'int' 0)
>>>>> ;
>>>>> ===================
>>>>>
>>>>> So, finally, our question is: why such a difference?
>>>>>
>>>>> That is, why does the translation unit context contains these 
>>>>> (implicitly instantiated) definitions of static data members?
>>>>> It was not containing the (implicitly instantiated) specialization of 
>>>>> the class template ... or of its methods.
>>>>
>>>> Definitely a bug. Implicit instantiations should not show up in the translation unit context.
>>>
>>> The attached patch should fix this, right? Should I commit it?
>>
>> Ping.
> 
> This looks fine to me.

Committed in r105465.




More information about the cfe-dev mailing list