Z
ztycho
Could someone explain if the following gcc 4.1 compile error is
working as designed and, if so, why does the second version of the
code not fail?
Failing case:
======================================
struct A
{
int GetId() const;
};
struct B
{
A& GetElement() const;
};
template<typename T>
int function(const B& b, const T&)
{
return dynamic_cast<const A&>(b.GetElement()).GetId();
}
test.cpp: In function 'int function(const B&, const T&)':
test.cpp:14: error: request for member 'GetId' in 'dynamic_cast<const
A&>(b->B::GetElement())', which is of non-class type 'const A&'
=========================================
Working case:
=========================================
struct A
{
int GetId() const;
};
struct B
{
A& GetElement() const;
};
template<typename T>
int function(const B& b, const T&)
{
const A& element = dynamic_cast<const A&>(b.GetElement());
return element.GetId();
}
=========================================
Any help would be appreciated
working as designed and, if so, why does the second version of the
code not fail?
Failing case:
======================================
struct A
{
int GetId() const;
};
struct B
{
A& GetElement() const;
};
template<typename T>
int function(const B& b, const T&)
{
return dynamic_cast<const A&>(b.GetElement()).GetId();
}
test.cpp: In function 'int function(const B&, const T&)':
test.cpp:14: error: request for member 'GetId' in 'dynamic_cast<const
A&>(b->B::GetElement())', which is of non-class type 'const A&'
=========================================
Working case:
=========================================
struct A
{
int GetId() const;
};
struct B
{
A& GetElement() const;
};
template<typename T>
int function(const B& b, const T&)
{
const A& element = dynamic_cast<const A&>(b.GetElement());
return element.GetId();
}
=========================================
Any help would be appreciated