Newsgroup: comp.lang.c++
Subject: Behavior of the local class
From: somenath <somenathpal@...>
Date: Thu, 2 Jan 2014 01:22:47 -0800 (PST)
I am not able to understand why the following program does not compile.
#include<iostream>
using namespace std;
template <class T >
void Fun(T t)
{
static int i = t;
class Test {
public:
Test( int t1) {
cout<<"Value = "<<i<<endl;
}
};
Test tt(t);
}
int main(void)
{
Fun<int >(5);
return 0;
}
Whenever I try to compile the program I get the following error
g++ LocalClass.cpp
/tmp/ccfEKvyT.o:LocalClass.cpp:(.text+0x2b): undefined reference to `i'
collect2: ld returned 1 exit status
But I am aware that local class can access the static local variable of enclosing function. The following program proves that as well.
#include<iostream>
using namespace std;
void Fun(int t)
{
static int i=t;
class Test {
public:
Test( int t1) {
cout<<"Value = "<<i<<endl;
}
};
Test tt(t);
}
int main(void)
{
Fun(5);
return 0;
}
This code compile fine. Then what is going wrong with template version of the program?
Subject: Behavior of the local class
From: somenath <somenathpal@...>
Date: Thu, 2 Jan 2014 01:22:47 -0800 (PST)
I am not able to understand why the following program does not compile.
#include<iostream>
using namespace std;
template <class T >
void Fun(T t)
{
static int i = t;
class Test {
public:
Test( int t1) {
cout<<"Value = "<<i<<endl;
}
};
Test tt(t);
}
int main(void)
{
Fun<int >(5);
return 0;
}
Whenever I try to compile the program I get the following error
g++ LocalClass.cpp
/tmp/ccfEKvyT.o:LocalClass.cpp:(.text+0x2b): undefined reference to `i'
collect2: ld returned 1 exit status
But I am aware that local class can access the static local variable of enclosing function. The following program proves that as well.
#include<iostream>
using namespace std;
void Fun(int t)
{
static int i=t;
class Test {
public:
Test( int t1) {
cout<<"Value = "<<i<<endl;
}
};
Test tt(t);
}
int main(void)
{
Fun(5);
return 0;
}
This code compile fine. Then what is going wrong with template version of the program?
via Usenet Forums - Usenet Search,Free Usenet - comp.lang.c++ http://www.pocketbinaries.com/usenet-forums/showthread.php?162857-Behavior-of-the-local-class&goto=newpost
View all the progranning help forums at:
http://www.pocketbinaries.com/usenet-forums/forumdisplay.php?128-Coding-forums
No comments:
Post a Comment