Sign in to follow this  
aayudh

porting code to gcc ..template code problems

Recommended Posts

i am having some problem porting some template code from windows compiler to gcc this is the function
template <class _T> inline impl::DeclaratorClass &BindBasicString(const std::string &_name)
{
return Class<_T>(_name)
.Constructor()
.Constructor<const _T&>()       //line 65
.Method("clear", &_T::clear)
.Method("empty", &_T::empty)
.Method("size", &_T::size)
.Constructor<const _T::value_type*>()
.Constructor<const _T::value_type*, _T::size_type>()
.Constructor<const _T&, _T::size_type>()
.Constructor<const _T&, _T::size_type, _T::size_type>()
.Constructor<_T::size_type, _T::value_type>()
.Method("c_str", &_T::c_str)
.Method("push_back", &_T::push_back)
.Operator(Str, &_T::c_str)
.Operator(const_self + const_self)
.Operator(const_self == const_self)
.Operator(const_self == other<const _T::value_type*>())
.Operator(other<const _T::value_type*>() == const_self)
.Operator(impl::BinaryOperatorBaseHolder<ConCat, true, true, const _T>())
.Operator(impl::BinaryOperatorBaseHolder<ConCat, true, true, const _T::value_type*>())
.Operator(impl::BinaryOperatorBaseHolder<ConCat, true, false, const _T::value_type*>())
.Enum("npos", _T::npos);
}

.....


template<class _Class>
struct CustomClass : GenericClass
{
	typedef CustomClass<_Class> Me;

.....
	template<class _P1> inline Me &Constructor(const GenericMethodPolicy &_p = GenericMethodPolicy(0)) {
		return InsertConstructor(new ConstructorClass1<_Class, _P1>(_p)), *this; };
};
The Constructor method below is what is called when .Constructor is called. Now you can download mluabind source code if you want to have a deeper look. now the errors I get in gcc are as such /usr/local/mluabind-0.05/impl/../impl/stl_basic_string.h: In function 'mluabind::impl:eclaratorClass& mluabind::BindBasicString(const std::string&)': /usr/local/mluabind-0.05/impl/../impl/stl_basic_string.h:65: error: expected primary-expression before 'const' /usr/local/mluabind-0.05/impl/../impl/stl_basic_string.h:65: error: expected ';' before 'const' /usr/local/mluabind-0.05/impl/../impl/stl_basic_string.h:65: error: expected unqualified-id before '>' token /usr/local/mluabind-0.05/impl/../impl/stl_basic_string.h:65: error: expected initializer before '>' to I am assuming gcc template programming is different. Can some understand and help me out with this !!!!

Share this post


Link to post
Share on other sites
Try instead of
.Constructor<const _T&>()       //line 65


this one:
.template Constructor<const _T&>()       //line 65


.

It may look funny, but with that you tell the compiler that the following thing is a template and not a comparison. (see Vandevoorde/Josuttis, Page 44, "The .template construct")

Btw, gcc is not different, but in this respect just more strict to the standards, where MSVC is more liberal.

Share this post


Link to post
Share on other sites
Hey.. the .template in conjunction with using typename t::something has currently stopped the errors. The function I posted now compiles with no errors.

I still have to solve a lot these errors in order to get working code.

Right now, I am having this issue.
I wanted to
typedef typename T _T;

so that I dont need to type 'typename' everywhere. But its giving me

error: nested-name-specifier before 'T'
error: expected initializer before '_T'

why does this happen ?
any suggestions ?

Share this post


Link to post
Share on other sites
typedef typename T _T;


Remember the basic syntax of typedef:
typedef <type> <alias>;


If am right, in your code _T would be the type, and T is supposed to be the alias. So it should go
typedef typename _T T;

.

Share this post


Link to post
Share on other sites
Quote:
Original post by aayudh
I wanted to
typedef typename T _T;

so that I dont need to type 'typename' everywhere.


Unfortunately, that typedef won't stop you from needing to use the typename keyword. T will still be a dependent type, since it depends on the type _T, so T::foo will still need the typename keyword to resolve as a type.

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
Quote:
Original post by aayudh
I wanted to
typedef typename T _T;

so that I dont need to type 'typename' everywhere.


Unfortunately, that typedef won't stop you from needing to use the typename keyword. T will still be a dependent type, since it depends on the type _T, so T::foo will still need the typename keyword to resolve as a type.


... but of course you could add more typedefs. While aliasing types is something I personally dislike, they can be handy on templates:
template <typename _T> struct Foo {
typedef _T T;
typedef typename T::Bar TBar; // 'typename' still needed, as it refers to the right hand side of the ::-operator.
TBar test; // Not needed.
};
int main () {}

Share this post


Link to post
Share on other sites
It's not
Quote:
typedef typename T _T;

but
typedef T _T;

(assuming T is a type, and _T an unused identifier in this scope)

I suggest you read about when (and, more than anything, *why*) to use the typename and template keywords.

Share this post


Link to post
Share on other sites
hey guys,

I am using typename where required. Lets not worry about typedef now.

Now there is a bigger problem.
I am having an explosion of these errors

error: forming pointer to reference type 'std::basic_string<char, std::char_traits<char>, std::allocator<char> >&'

Now, the only way I can ask for help is by posting my kdevelop project completely since I dont know what code to post.

For now I will post what I can.



//line that caused error
template <class _Ret> inline Me &Method(const char* _name, _Ret (_Class::*_F) (), const GenericMethodPolicy &_p = GenericMethodPolicy(0) ) {
return InsertMethod(_name, new MethodClass0<_Class, false, _Ret>(_name, _F, _p)), *this; };




/usr/local/mluabind-0.05/impl/../impl/class.h: In instantiation of 'mluabind::impl::CustomClass<std::basic_string<char, std::char_traits<char>, std::allocator<char> >&>':
/usr/local/mluabind-0.05/impl/../impl/util.h:527: instantiated from 'static mluabind::impl::String mluabind::impl::ParameterVerboseInfo<P, 0>::Info(mluabind::CHost*) [with P = const std::basic_string<char, std::char_traits<char>, std::allocator<char> >&]'
/usr/local/mluabind-0.05/impl/../impl/util.h:599: instantiated from 'static mluabind::impl::String mluabind::impl::ParametersVerboseInfo2<T1, T2>::Info(mluabind::CHost*) [with T1 = const std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, T2 = unsigned int]'
/usr/local/mluabind-0.05/impl/../impl/constructor.h:135: instantiated from 'mluabind::impl::ConstructorClass2<_Class, _P1, _P2>::ConstructorClass2(const mluabind::impl::GenericMethodPolicy&) [with _Class = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _P1 = const std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, _P2 = unsigned int]'
/usr/local/mluabind-0.05/impl/../impl/class.h:173: instantiated from 'mluabind::impl::CustomClass<_C>& mluabind::impl::CustomClass<_C>::Constructor(const mluabind::impl::GenericMethodPolicy&) [with _P1 = const std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, _P2 = unsigned int, _Class = std::basic_string<char, std::char_traits<char>, std::allocator<char> >]'
/usr/local/mluabind-0.05/impl/../impl/stl_basic_string.h:152: instantiated from 'mluabind::impl::DeclaratorClass& mluabind::BindBasicString(const char*) [with _T = std::basic_string<char, std::char_traits<char>, std::allocator<char> >]'
/Projects/Gaaya/KDevelop/external_libs/mluabind-0.05/stdbinds.cpp:40: instantiated from here
/usr/local/mluabind-0.05/impl/../impl/class.h:61: error: forming pointer to reference type 'std::basic_string<char, std::char_traits<char>, std::allocator&

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this