I bought the source code for an Ogre Plugin (Particle Universe) which is (or should´ve been) written in platform independent C++. It comes with a Visual Studio solution which compiles just fine, but because my whole project is compiled with G++, I want to compile that library with G++ too. But for some reason a template declaration throws an error in G++. Here is the code snippet.
typedef Ogre::Any Any;
template <typename ValueType> ValueType* any_cast(Any* operand) : public any_cast(operand){};
Is any C++ guru able to tell me why this error shows up? (maybe different syntax?)
include/ParticleUniverseAny.h: In function 'ValueType*
ParticleUniverse::any_cast(ParticleUniverse::Any*)':
include/ParticleUniverseAny.h:21:68: error: only constructors take member initializers
include/ParticleUniverseAny.h:21:68: error: expected identifier before 'public'
include/ParticleUniverseAny.h:21:68: error: expected '{' before 'public'
include/ParticleUniverseAny.h: At global scope:
include/ParticleUniverseAny.h:21:68: error: expected unqualified-id before 'public'
I would be happy and thankful for any help!
EDIT:
any_cast is defined in OgreAny.h
friend ValueType * any_cast(Any *);
template<typename ValueType>
ValueType * any_cast(Any * operand)
{
return operand && operand->getType() == typeid(ValueType)
? &static_cast<Any::holder<ValueType> *>(operand->mContent)->held
: 0;
}
template<typename ValueType>
ValueType * any_cast(Any * operand)
{
return operand && operand->getType() == typeid(ValueType)
? &static_cast<Any::holder<ValueType> *>(operand->mContent)->held
: 0;
}
template<typename ValueType>
const ValueType * any_cast(const Any * operand)
{
return any_cast<ValueType>(const_cast<Any *>(operand));
}
template<typename ValueType>
ValueType any_cast(const Any & operand)
{
const ValueType * result = any_cast<ValueType>(&operand);
if(!result)
{
StringUtil::StrStreamType str;
str << "Bad cast from type '" << operand.getType().name() << "' "
<< "to '" << typeid(ValueType).name() << "'";
OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS,
str.str(),
"Ogre::any_cast");
}
return *result;
}
This code is complete nonsense:
It looks like a template function definition with the:
part. But then there’s a colon, which is only legitimate in a constructor initialization list, followed by
public, which is only legitimate in a class declaration. The two tokens:andpublic:simply can’t be present together like this.There must be some macro black magic going on here which makes in compile under MSVC and isn’t working under GCC. If so, this is a prime example of why macros can be evil. When misused, they result in a new secret language that nobody but the author knows.
Unless you can get direct support from the library’s authors, you are likely up a creek with respect to getting this to compile under GCC.