W
Werner
Hi All,
The following code fails to compile on GCC 4.7.
I've now learned because of its compliance. I've
also noticing it failing under Comeau. On moving
the functions in the anonymous namespace above the
definition of Xx, it compiles:
------------------------------------------------
template <class T>
struct Xx
{
Xx()
{
T inst;
int n = sequenceSize( inst );
}
};
struct Header1
{
int sequenceSize_;
};
struct Header2
{
int itemCount_;
};
namespace {
int sequenceSize( const Header1& h )
{
return h.sequenceSize_;
}
int sequenceSize( const Header2& h )
{
return h.itemCount_;
}
}
void testUnqualifiedLookup()
{
Xx<Header1> xH1;
Xx<Header2> xH2;
}
Now (the question):
What would be the best way to fix this kind of problem?
A couple of options exist:
1) Functions sequenceSize would be found if they were
in the same namespaces as classes "Headerx" (by ADL).
2) A declaration of sequenceSize need at least exist
prior to the template definition.
I now have a case like this:
struct MsgX
{
int sequenceSizeOfA_;
int sequenceSizeOfB_;
std::vector<A> a_;
std::vector<B> b_;
};
I still would like the calling code (in the template
to work, therefore:
//Translation Unit A:
namespace {
int sequenceSize( const MsgX& msg )
{
return msg.sequenceSizeOfA_;
}
}
#include "Xx.h"
//Translation Unit B:
namespace {
int sequenceSize( const MsgX& msg )
{
return msg.sequenceSizeOfB_;
}
}
#include "Xx.h"
.... but this makes the compilation dependent on
the order of inclusion. Also, in this case
I cannot use ADL, because in both instances
of sequenceSz, the argument type is the same.
Now the question:
1) Is it bad of Xx to depend on an unqualified name?
2) If I'm not able to change Xx, what would be a good
solution - to include the declaration of sequenceSize
above Xx?
3) If I am allowed to change Xx, what would you do?
Kind regards,
Werner
The following code fails to compile on GCC 4.7.
I've now learned because of its compliance. I've
also noticing it failing under Comeau. On moving
the functions in the anonymous namespace above the
definition of Xx, it compiles:
------------------------------------------------
template <class T>
struct Xx
{
Xx()
{
T inst;
int n = sequenceSize( inst );
}
};
struct Header1
{
int sequenceSize_;
};
struct Header2
{
int itemCount_;
};
namespace {
int sequenceSize( const Header1& h )
{
return h.sequenceSize_;
}
int sequenceSize( const Header2& h )
{
return h.itemCount_;
}
}
void testUnqualifiedLookup()
{
Xx<Header1> xH1;
Xx<Header2> xH2;
}
Now (the question):
What would be the best way to fix this kind of problem?
A couple of options exist:
1) Functions sequenceSize would be found if they were
in the same namespaces as classes "Headerx" (by ADL).
2) A declaration of sequenceSize need at least exist
prior to the template definition.
I now have a case like this:
struct MsgX
{
int sequenceSizeOfA_;
int sequenceSizeOfB_;
std::vector<A> a_;
std::vector<B> b_;
};
I still would like the calling code (in the template
to work, therefore:
//Translation Unit A:
namespace {
int sequenceSize( const MsgX& msg )
{
return msg.sequenceSizeOfA_;
}
}
#include "Xx.h"
//Translation Unit B:
namespace {
int sequenceSize( const MsgX& msg )
{
return msg.sequenceSizeOfB_;
}
}
#include "Xx.h"
.... but this makes the compilation dependent on
the order of inclusion. Also, in this case
I cannot use ADL, because in both instances
of sequenceSz, the argument type is the same.
Now the question:
1) Is it bad of Xx to depend on an unqualified name?
2) If I'm not able to change Xx, what would be a good
solution - to include the declaration of sequenceSize
above Xx?
3) If I am allowed to change Xx, what would you do?
Kind regards,
Werner