S
Stu
I have some C++ code that has been running on various versions of
Microsoft Visual C++ for several years. I am now trying to get it
running on Unix, specifically gcc. I have hit two snags. The first is
quite simple: #pragma once is obsolete. Was this replaced by
something better, or do we have to go back to the old #ifndef nnn_h
trick?
The second problem is that I did something that is apparently not
allowed by the ISO standard. Consider the following code:
template<class TContainer>
void WriteSequence(
std:stream& aStream,
const TContainer& aContainer)
{
bool isFirst = true;
TContainer::const_iterator iter;
for (iter=aContainer.begin(); iter!=aContainer.end(); iter++)
{
if (isFirst)
isFirst = false;
else
aStream<<",";
aStream<<*iter;
}
}
Yeah, I know: useless code, but it does illustrate an issue. The hope
was that if WriteSequence<T> were instantiated with a type T that
defined T::const_iterator then all would be well. If T didn't, then
there would be a compile error at the point of instantiation. MS VC++
was apparently cool with this, which implies that it binds its
templates to code later than does gcc. From what I have heard, gcc is
almost always a closer implementation of the standard, so I assume this
is nonstandard.
If this can't be done within the standard, is there some nice legal way
of doing the same thing? The sticking point seems to be that each of
the containers is a separate class with no common superclass that
defines const_iterator.
Microsoft Visual C++ for several years. I am now trying to get it
running on Unix, specifically gcc. I have hit two snags. The first is
quite simple: #pragma once is obsolete. Was this replaced by
something better, or do we have to go back to the old #ifndef nnn_h
trick?
The second problem is that I did something that is apparently not
allowed by the ISO standard. Consider the following code:
template<class TContainer>
void WriteSequence(
std:stream& aStream,
const TContainer& aContainer)
{
bool isFirst = true;
TContainer::const_iterator iter;
for (iter=aContainer.begin(); iter!=aContainer.end(); iter++)
{
if (isFirst)
isFirst = false;
else
aStream<<",";
aStream<<*iter;
}
}
Yeah, I know: useless code, but it does illustrate an issue. The hope
was that if WriteSequence<T> were instantiated with a type T that
defined T::const_iterator then all would be well. If T didn't, then
there would be a compile error at the point of instantiation. MS VC++
was apparently cool with this, which implies that it binds its
templates to code later than does gcc. From what I have heard, gcc is
almost always a closer implementation of the standard, so I assume this
is nonstandard.
If this can't be done within the standard, is there some nice legal way
of doing the same thing? The sticking point seems to be that each of
the containers is a separate class with no common superclass that
defines const_iterator.