c++/7308: template-class member functions / namespace scope resolution in conjunction with template specialization

a9804814@unet.univie.ac.at a9804814@unet.univie.ac.at
Sun Jul 14 08:57:00 GMT 2002

>Number:         7308
>Category:       c++
>Synopsis:       template-class member functions / namespace scope resolution in conjunction with template specialization
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          accepts-illegal
>Submitter-Id:   net
>Arrival-Date:   Sun Jul 14 08:56:01 PDT 2002
>Originator:     a9804814@unet.univie.ac.at
>Release:        gnu 2.95.3-6 (mingw special)


The following short program consists of a template definition in a header-file, and a program file (driver program) which instantiates the template class.

The definitions in template-classes can be grouped into type-dependent definitions (operation using the templated type) and type-independent types (operations which do not require the type of the template parameters, like the member function "Print_Name" below (I know, this should be a static member function, but lets concentrate on the bug).

The declaration of operands in type-independent operations must be known at the time these members are DEFINED (and not when the class'es / member function's point-of-instantiation is reached). In fact, the function "Print_Name" uses std::cout and std::endl as (type-independent) operands. 
However, none of these is #included before the definition of the template class.

The definition of this function should be in error, because <iostream> is not #included before the definition of the function ends.

Here the program:

// in header-File "name_resolution.hpp"


template <typename T>
class Test_Class

      // should be in error, as type-independent operands 
      // std::cout and std::endl aren't yet declared
      void Print_Name() const {std::cout << "test_class" << std::endl;}

   	T Object__;


// in program file

#include "name_resolution.hpp"

#include <iostream>

int main(int argc, char* argv[])
	Test_Class<int> TClass;


   return 0;


When template classes are defined in namespaces, then all specialization have to be declared WITHIN THE SAME NAMESPACE (the definitions of the specialization can be defined in other namespaces).
In the program below, namespace "Inner_Name_Space", which is itself a member of namespace "Outer_Name_Space", defines template class, and does not declare nor define any specializations.
However, in "Outer_Name_Space" a specialized function is defined. In main(), this specialized function is then used.
This should not be the case, as only specializations declared within exactly the same namespace scope as the template-class definition itself are accepted / considered during name resolution.

#include <iostream>

namespace Outer_Name_Space

	namespace Inner_Name_Space

      template <typename T>
      class Test_Class
            Test_Class(const T & Init) : Value_(Init) {}

            const T & Value() const {return Value_;}
            void Value(const T & New_Value) {Value_ = New_Value;}

     			T Value_;


   template <>
   Inner_Name_Space::Test_Class<int>::   Value(const int & New_Value)
   	std::cout << "setting new 'int' - Value." << std::endl;
      Value_ = New_Value;



int main(int argc, char* argv[])
   using Outer_Name_Space::Inner_Name_Space::Test_Class;

   Test_Class<int> A(745);

   // calls specialized member function, but shouldn't
   	cout << A.Value();

   return 0;

best regards,

Thomas Mang




More information about the Gcc-bugs mailing list