How can I return a generic type without templates in C++?

c template return type
class templates in c++ with simple example
template <typename vs class
generic function in c
c++ template class constructor
c++ template return value
c++ template class definition
c generic return type

I am looking to create a method that can return a generic type and then this generic type can be handled by other methods that I have specifically defined.

PCCommand CrossPlatform::GetCommandPC();
XboxCommand CrossPlatform::GetCommandXbox();

? CrossPlatform::GetCommand()
{
    #if PLATFORM == 1
        //PC
        return GetCommandPC();
    #elif PLATFORM == 2
        //xbox
        return GetCommandXbox();
    #endif

}

Both GetCommandPC and GetCommandXbox return different types, so how would I return them as a generic type in C++? The issue with templates, unless I'm mistaken would be that I'd have to pass a type into this function, but I wouldn't know what type to pass as the preprocessor checks would determine what platform is being used.

At the moment the PCCommand and XboxCommand are enum classes, so I'm not sure if it's possible to use a base type with enum classes.

Is there any way to achieve this, or a better way to achieve this?

Since you are using conditional compilation, and assuming you only need one of the types (i.e. the one for the platform you are running on); the cleanest thing you can do is having a single Command type, which is the correct one in each case:

enum class Command
{
    #if PLATFORM == 1
        // PC
        // ...
    #elif PLATFORM == 2
        // xbox
        // ...
    #endif
};

Think about it this way: when you are on e.g. PC, all the code for the other platforms is dead code (it won't be used, it won't be compiled, it might not even be able to be compiled!). And a general good practice is avoiding dead code lying around!

Generic Types in Java, C++, and C, This is why Java's templates cannot support primitive types, because a single copy of Generic code uses the type Object whenever it needs to refer to a specific for "Type" protected T t; public void add(T t) { this.t = t; } public T get() { return t; } } < or >), there is no way to explicitly declare that requirement in the template,  Function templates are special functions that can operate with generic types. This allows us to create a function template whose functionality can be adapted to more than one type or class without repeating the entire code for each type. In C++ this can be achieved using template parameters. A template parameter is a special kind of parameter that can be used to pass a type as argument: just like regular function parameters can be used to pass values to a function, template parameters allow

For cross platform types that are controlled by #defines:

PCCommand CrossPlatform::GetCommandPC();
XboxCommand CrossPlatform::GetCommandXbox();

#if PLATFORM == 1
    typedef PCCommand CrossPlatformCommand;
#elif PLATFORM == 2
    typedef XboxCommand CrossPlatformCommand;
#endif

CrossPlatformCommand CrossPlatform::GetCommand()
{
    #if PLATFORM == 1
        //PC
        return GetCommandPC();
    #elif PLATFORM == 2
        //xbox
        return GetCommandXbox();
    #endif

}

Templates - C++ Tutorials, Function templates are special functions that can operate with generic types. adapted to more than one type or class without repeating the entire code for each type. template < class myType> myType GetMax (myType a, myType b) { return​  You need pass a type that is type casteable for the value you return through that method. If you would want to return a value which is not type casteable to the generic type you pass, you might have to alter the code or make sure you pass a type that is casteable for the return value of method. So, this approach is not reccomended.

You can use inheritance. Create a super class Command and make PCCommand and XboxCommand subclasses and then in your function return a Command.

Overloads and templates - C++ Tutorials, Note that a function cannot be overloaded only by its return type. C++ has the ability to define functions with generic types, known as function templates. It makes no difference, and T is actually a quite common template parameter name for  As the return type of the Data property in the nested Node class. As the type of the private member data in the nested class. Note that T is available to the nested Node class. When GenericList<T> is instantiated with a concrete type, for example as a GenericList<int>, each occurrence of T will be replaced with int.

Generic Functions (C++/CLI), return-type. The type returned by the method. If the return type is void, no return value is required. identifier. The function name. type-parameter  The closest to C++ templates in C# is generics - but they're not very close. In particular, you can't use operators like > between generic type values, because the compiler doesn't know about them (and you can't constrain types based on operators).

Generic Types · phan/phan Wiki · GitHub, All template types for a generic class must be declared on the class via doc block definition of C from the example above to show how to extend a generic class. and methods can refer to them via @var , @param and @return . Phan will take the union of all types inferred for T (without checking if the  (Yes, this is legal code; see Java Generics: Generic type defined as return type only.) The return type will be inferred from the caller. However, note the @SuppressWarnings annotation: that tells you that this code isn't typesafe. You have to verify it yourself, or you could get ClassCastExceptions at runtime.

What did people do before templates in C++?, The traditional way to implement generics without having generics (the Because item encapsulates a void pointer, any data type can be stored there. const double *db = (const double *) b; return (*da > *db) - (*da < *db); }. Templates in C++ A template is a simple and yet very powerful tool in C++. The simple idea is to pass data type as a parameter so that we don’t need to write the same code for different data types.

Comments
  • You can use void pointers (void *), but you should cast to proper type before accesing content.
  • @aicastell Please, do not recommend using void * in C++ (assuming this is not absolutely necessary). It is dramatically type-unsafe and error-prone.
  • I would change them to classes if enums do not support inheritance.
  • That's how I would do it too (and make GetCommand contain the body of the code separate by an ifdef), but the presence of GetCommandPC and GetCommandXbox suggest those functions do exist, and need a return type.
  • @MartinBonner: Agreed -- with this I tried to answer the "a better way to achieve this?" part of OP's question.
  • Similar to my answer, but I would go further avoiding to define/declare the unused types at all (and their related functions).