0

I know there are easier ways to do this, but this is what I am asking.

Suppose you have one template function, function1, and another template function, function2.

The function definitions are as follows:

template <typename A>
void function1(A x) // typename A is part of a class-template function
                    // the typename is declared in the class instantiation
                    // and passed to function2

template <typename B>
void function2(B y) // I know I can use typeinfo(y).name to get name
                    // this returns a const char* 'm' 
                    // 'm' stands for unsigned long on my sytem

The reference for the 'm' assertion is here: Strange output of std::typeid::name()

As I said, I know it is possible to figure out (deduce) the parameter a function receives with

const char* x = typeinfo(parameter).name;
// returns const char* 'm' on my machine

Is it possible, if a function receives a generic parameter, to also instantiate a new object of the same type. Something like:

<x> foo;
// where x represents the const char* = 'm'
// which in turn is represented by unsigned long on my system
// so foo would be an uninstantiated unsigned long identifier

I see here: Creating a new object from dynamic type info that it isn't possible for objects, but I am wondering if it is possible for internal types like int.

Thanks!

Free Url
  • 1,836
  • 2
  • 15
  • 28
  • Fortunately, modern C++ makes it quite easy to lose bad programming habits, like "generic parameters", and replace them with type-safe, compiler-optimized templates. Maybe you've heard of them? – Sam Varshavchik Oct 11 '17 at 23:10
  • That's not what is commonly known as type deduction. –  Oct 11 '17 at 23:12
  • Say you do determine you have a `const char*`. How do you determine the length of what it points at in order to create another? – user4581301 Oct 11 '17 at 23:13
  • Yes I am implementing a template function that receives an object that is itself, based on another template function. The functions are separate and I am trying to accomplish this task in the second template function. Normally, I would make them helper functions and not worry about it but I can't in this case. – Free Url Oct 11 '17 at 23:14
  • If this is a template function, and a template parameter, and this is what you're trying to do, you're doing it wrong. There's no reason to go through acrobatics to make a copy of the object. Just copy it, and let the template code do its work. – Sam Varshavchik Oct 11 '17 at 23:15
  • It isn't my choice to do it this way, I am just asking if it can be done. I will clarify the original post shortly. – Free Url Oct 11 '17 at 23:16
  • Note that when you say "isn't possible for objects" that already covers builtin types. C++ primitive types are not "classes", but instances of them do fall under "objects" – Ben Voigt Oct 11 '17 at 23:31
  • fair enough Ben, I was afraid this might be the case, but didn't know enough about that level of implementation to be sure. – Free Url Oct 11 '17 at 23:34

2 Answers2

0

Use templates for this?

template <typename T> 
void foo(T yourParam)
{
    T newVar;  //Use of the deduced type to declare new variable
}

You can also use decltype:

decltype(parameter) newVar;

As you seem insistent on using the const char *, and you're asking about builtin types, why not use a switch statement?

switch(x)
{
    case 'm' : {
        unsigned long foo;
        //functionality
        break;
    }
    //other cases
    default : break;
}
nitronoid
  • 1,459
  • 11
  • 26
  • Good try, but this doesn't scale, and the whole point of using template functions is for them to scale to different types. If I had any clue what the input would be I would try this. Thanks for the attempt. – Free Url Oct 11 '17 at 23:41
0

You can extract the type from any expression using decltype, for example using that to declare another variable:

decltype(1 + 3.14159) e;
e = 2.7182818;

But this works on compile-time type information ("static" typing). There's no way to do it based on dynamic type, nor to use runtime type data (typeinfo that typeid returns, or any other object you can capture at runtime). At best you could grab the type name, write out a C++ source file, and invoke the C++ compiler on it.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • You could also write a virtual `clone` method in the base type, and have override it in each derived type. – Daniel H Oct 11 '17 at 23:32
  • Thanks Ben, even though I had already seen decltype, I guess this cannot be done. But your answer got me closest. Is it protocol to accept the answer or leave the question opened, or should someone post an answer saying this can't be done? Thanks again! – Free Url Oct 11 '17 at 23:39