Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 823
  • Last Modified:

Template inside a function definition

What is the reason we cannot have a template declaration inside a function definition?

Why this piece of code not compile?

void displayClass()
{
template TestPkg< typename T >
class Pkg
{
      public:
            T iVal;
            void Display()
                  {
                        cout << "Value:" << iVal << endl;
                  }
};
int i =10;
TestPkg< int > sample( i );
}

int main()
{
displayClass();
return 0;
}

I am interested in understanding as to what happens inside the compiler, which is restricting such a declaration?

IL.
0
logically_illogical
Asked:
logically_illogical
  • 4
  • 2
  • 2
  • +1
3 Solutions
 
Dariusz DziaraProgrammerCommented:
C2951: template declarations are only pertmitted at global or namespace scope
0
 
Dariusz DziaraProgrammerCommented:
template TestPkg< typename T >
class Pkg
{
[...]
};

or

namespace MyNamespace {
template TestPkg< typename T >
class Pkg
{
[...]
};
};

will compile
0
 
Dariusz DziaraProgrammerCommented:
And from MSDN ("Template Specifications" topic):

"Template declarations have global or namespace scope."
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
efnCommented:
Nothing happens inside the compiler to restrict such a declaration.  There is no technical reason why such declarations could not be legal.  It was a design decision by the designer of the language.  Possibly he thought the benefit of allowing such declarations was not worth the burden on compiler implementers.
0
 
bachra04Commented:
Because Template has an external linkage and local scope elements have internal linkage.

"A forward declaration of a template instantiation has the form of an explicit template instantiation preceded by the extern keyword.

>>-extern--template--template_declaration----------------------><"

This is taken from IBM site.
http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=/com.ibm.vacpp6m.doc/language/ref/clrc01cplr082.htm

B.T
 
 
0
 
logically_illogicalAuthor Commented:
I think I have not clearly laid down my question:
I guess the following would help:

I need to know whether:
1. Not allowing template declaration inside a method/function was a design consideration or not?
2. If Yes, then what was the reason for not allowing such a functionality?

I know it is not allowed but there might be some reason to this, that is what I was looking for.
This is just a mere query.
Any references leading to the same would be appreciated ( and of course would be awarded points :) )

IL.
0
 
Dariusz DziaraProgrammerCommented:
I have not found explicit explaination but I would bet it is by design as "efn" suggested.
I can image myself local template declaration that can be instantiated locally only (and why not ?). I cannot imagine that all C++ implementations have the same limitation - impossible local template declarations.

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1801.htm (looks like ANSI spec.)

"A template-declaration can appear only as a namespace scope or class scope declaration"

http://www.csci.csusb.edu/dick/c++std/cd2/template.html

"A template-declaration  can appear only as a namespace scope or class
  scope declaration."

http://www.lnf.infn.it/Calcolo/doc/aixcxx/html/language/ref/rntmpsyn.htm

"A template declaration can appear as a global declaration only. (only global ?)"

http://www.unl.csi.cuny.edu/faqs/g++-faq/wp/sep94/template.html

"template-declaration can appear only as a global declaration,  as  a
member  of  a  namespace,  as a member of a class, or as a member of a
class template."


0
 
bachra04Commented:
cLASS Templates are here to improve the design by allowing to reuse
the same algorithm for different types (int, double...) or
different classes (Class A, class b...). This is done by creating a class Template
and pass it as parameter toi function that can be called elsewhere by just passing
The required Type or class.

Now, lets suppose we can create a Template class locally,
doing so results in a very bad design decision, in other words give me
just one good reason to declare a class Template locally ?

I.E. What you can do by using a local Template class can be done differently
but in more efficient and elegant way.

So, I think this is a design decision and there is a good reason for that.
0
 
efnCommented:
The reason might be addressed in the book "The Design and Evolution of C++" by Bjarne Stroustrup.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now