Jump to content
  • Advertisement
Sign in to follow this  
Zakwayda

Parameterization by return type

This topic is 5399 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've recently found it would be convenient to write a function like this:
template <class T> T* NewObject()
{
    return new T;
}

// Usage
int* myint = NewObject();


In other words, the parameterization is determined by the return type only. I tried it, and my compiler said 'no', so I figured it just wasn't part of the language. But then I read something that seemed to suggest that some newer compilers support this. Is this true? Is it a feature of the language that is supported by more up-to-date compilers? I tried it in Mac OSX on (what I think is) the newest version of XCode, but it didn't work. So anyway, I'm curious as to whether any compilers support this, or if it's just not part of the language.

Share this post


Link to post
Share on other sites
Advertisement

template <class T> T* NewObject(T* t = NULL)
{
return new T;
}

// Usage
int* myint = NewObject();



should solve the problem.

Share this post


Link to post
Share on other sites
This would be on the same rank as determining return type with overloading:

int Pie( void );
char Pie( void );

char pievalue = Pie();


which as of yet hasn't been implemented in any C++ compiler that I'm aware of due to the extreme programming that would be required for any case that the return value was not being imediately assigned to a value.

And in the cases where it WAS immediately assigned to a value ONLY (which would be the requirement of allowing this syntax failing the ability to guess the type correctly) there is a very simple workaround:

template <class T> void NewObject( T*& pointer ) //or is that T&*? Only 1 will compile
{
pointer = new T;
}

// Usage
int* myint;
NewObject( myint );





for which there allready compiler code in place to handle.

Quote:
Originally posted by nmi
should solve the problem.


No, it dosn't, and it shouldn't.

main.cc

#define NULL (int *)0 //exact same results with "(void *)0" and "0".
template <class T> T* NewObject(T* t = NULL)
{
return new T;
}

int main ( int argc , char ** argv )
{
int* myint = NewObject();
}


make -k all 
g++ main.cc -o main
main.cc: In function `int main(int, char**)':
main.cc:9: error: no matching function for call to `NewObject()'
make: *** [main] Error 1
make: Target `all' not remade because of errors.


There is no code in any compiler I am aware of that has any means for providing discrimination between functions based on return type. Without that, even with a differentiating argument type, there is no way for the compiler to guess the type of T with that default argument. I had originally thought if that even compiled, that the results would be: NewObject() returning a void* (since NULL is sometimes defined as (void *)0, and the only default result I can think of by casting an integer to "a pointer" of undefined type).

That would most likely cause a compile time error, trying to implicitly cast from void* to int*.

OP: It's just not part of the language. If you're worried about the type changing later on (can't see why this would happen for this code) you could do something like this:

template <class T> T* NewObject()
{
new T;
}
int main ( int argc , char ** argv )
{
int *intptr;
char *charptr;
intptr = NewObject<typeof(*intptr)>();
charptr = NewObject<typeof(*charptr)>();
}


Assuming your compiler supports the typeof keyword (which may be __typeof__ or something like that with some settings... this code compiles with gcc 3.3.1 cygwin/mingw version. With that version of GCC, even this works:

template <class T> T* NewObject()
{
new T;
}
int main ( int argc , char ** argv )
{
int *intptr = NewObject<typeof(*intptr)>();
char *charptr = NewObject<typeof(*charptr)>();
}


Which I would have thought wouldn't, as I would expect tokens to be declared after the finishing statement, not during.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!