Declaring struct/class local to a function
Jun 4, 2011 at 6:54am UTC
Hi,
Is it correct to declare a class local to any function.
Suppose I have a class like :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
class AnyClass
{
public :
void FunctionA();
};
void AnyClass::FunctionA()
{
struct CompareLess
{
bool operator () (const juce::String &lhs, const juce::String &rhs) const
{
bool retVal = false ;
int result = lhs.compareLexicographically(rhs);
if (result < 0)
retVal = true ;
else
retVal = false ;
return retVal;
}
};
std::set<juce::String, CompareLess> words;
}
Again, I working fine in for-granted Visual studio, But getting error in XCODE IDE(gcc). I'am not saying that gcc is wrong but I wants to know my mistake, what I am doing wrong?
It will be greatly appreciable if someone explain how compiler work for this piece of code?
Thanks
Vivek
Jun 4, 2011 at 6:55am UTC
One more things, If I write this CompareLess struct outside this function then it's working fine!
Jun 4, 2011 at 7:28am UTC
The (current) C++ standards documents does say that local classes aren't allowed to be
used in this way - but C++0X will remove that restriction.
So GCC is faithful to the current letter of the law so to speak.
But this restriction was always considerd too strict - See here (and numerous othr places)
http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=420
Last edited on Jun 4, 2011 at 7:33am UTC
Topic archived. No new replies allowed.