使用标准类型的动态类型信息实例化标识符

时间:2022-04-28 16:56:27

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.

假设您有一个模板函数function1和另一个模板函数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()

'm'断言的引用在这里: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.

我在这里看到:从动态类型信息创建一个对象不可能的新对象,但我想知道是否有可能像int这样的内部类型。

Thanks!

2 个解决方案

#1


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:

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?

由于您似乎坚持使用const char *,并且您在询问内置类型,为什么不使用switch语句?

switch(x)
{
    case 'm' : {
        unsigned long foo;
        //functionality
        break;
    }
    //other cases
    default : break;
}

#2


0  

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

您可以使用decltype从任何表达式中提取类型,例如使用它来声明另一个变量:

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.

但这适用于编译时类型信息(“静态”输入)。没有办法基于动态类型,也不使用运行时类型数据(typeid返回typeid,或者你可以在运行时捕获的任何其他对象)。最好你可以获取类型名称,写出一个C ++源文件,并在其上调用C ++编译器。

#1


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:

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?

由于您似乎坚持使用const char *,并且您在询问内置类型,为什么不使用switch语句?

switch(x)
{
    case 'm' : {
        unsigned long foo;
        //functionality
        break;
    }
    //other cases
    default : break;
}

#2


0  

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

您可以使用decltype从任何表达式中提取类型,例如使用它来声明另一个变量:

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.

但这适用于编译时类型信息(“静态”输入)。没有办法基于动态类型,也不使用运行时类型数据(typeid返回typeid,或者你可以在运行时捕获的任何其他对象)。最好你可以获取类型名称,写出一个C ++源文件,并在其上调用C ++编译器。