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.
logically_illogicalAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.