Re: doubt on non-type template-parameter
On 30 Mrz., 17:40, Bharath <tiromarc...@gmail.com> wrote:
I'm trying to go thru the non-type template-parameter rules in draft c+
+ standard. Here is one example which I tried to execute.
#include<iostream>
template <int *p> class MyArr
{
public:
MyArr();
};
MyArr::my_print()
{
std::cout<<"Hello World\n";}
my_print has no declaration and the out-of-class
definition is invalid. Lets fix that first:
template <int *p> class MyArr
{
public:
MyArr();
void my_print();
};
template <int* p>
void MyArr<p>::my_print()
{
std::cout<<"Hello World\n";
}
main ()
This is an invalid main definition. You must specify the
return type int.
{
int a[100];
MyArr<a> b_arr;
b_arr.my_print();
system ("pause");
}
As per the standard it says that - A non-type template-parameter of
type "array of T" is adjusted to be of type "pointer to T".
I'm getting below error while trying to compile the above program. Can
anyone please explain me the compiler errors in this and why they are
coming?
The main relevant part of the compiler output here is that
the template argument is invalid. Indeed it is invalid, because
according to [temp.arg.nontype]/1, bullet 3:
"- the address of an object or function with external linkage[..]"
Your entity a is an automatic variable w/o which has no linkage.
A simple fix would be something like this:
[..]
int a[100];
int main()
{
MyArr<a> b_arr;
[..]
}
HTH & Greetings from Bremen,
Daniel Kr?gler
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]