-1

In c++, typeid can accept the class name as the input. If I also want to implement a similar function, just like:

void func(T class)
{
  std::cout<< typeid(class).name() <<std::endl;
}

What should be the T?


Update

I am sorry for this unclearing question. And more details are provided in the following.

I want to use it as:

class A
{
};

void main()
{
    func(A);
}

I want func(A) can print the name of A just like std::cout<< typeid(A).name() <<std::endl;.

9
  • Do you mean that you want to call the function with an object or with the mangled name of a type? Commented Aug 26, 2022 at 8:29
  • 1
    You probably want a template. Do you know what templates are in C++? If not you should research that topic. Commented Aug 26, 2022 at 8:34
  • 1
    What @churill said. Is this what you want? Commented Aug 26, 2022 at 8:35
  • You can't define a function that takes a type as a parameter. Commented Aug 26, 2022 at 8:39
  • 1
    I don't really understand what you want to achieve. The question should show some example of how you want to call this function and what you expect the output to be in each case. Please edit it to add this information. Probably(?) unrelated to the actual question: class is a keyword in C++. It can't be used as the name of variables or other things. Commented Aug 26, 2022 at 8:42

2 Answers 2

4

how to define a function using class name as the input parameters

Functions do not take types as parameters.

I know what template is, and what I want is not template. I hope the function can be used as func(A), where A is a class.

typeid is not a function, it is a built-in operator. How it is implemented is beyond your reach.


What you can do is write a function template that can be instantiated with different types:

template <typename T>
void func() {
    std::cout<< typeid(T).name() <<std::endl;
}

This is a template and func<A> is a function (a specialization of the template) that can be called like this:

func<A>();  

Or if you like to keep the signature of your func:

template <typename T>
void func(const T& t) {
    std::cout<< typeid(t).name() <<std::endl;
}

Then you can call it like this:

SomeType t;
func(t);
Sign up to request clarification or add additional context in comments.

3 Comments

What do you mean by typeid is an operator? How to implement myself func operator? Could you please provide an example?
@yingzi You can overload some existing operators but you can't invent your own operators or overload typeid.
@yingzi sorry, not sure how to clarify more. I mean just that: typeid is an operator. "How to implement myself func operator?" you can't
1

An alternative if you really don't want to use the much nicer func<A>() as suggested in this answer could be to make a macro. It won't make func into an actual function. It's more or less a "search and replace" instruction to the preprocessor.

#include <iostream>
#include <typeinfo>

#define func(T) do { std::cout << typeid(T).name() <<std::endl; } while(false)

This can now be used like you wanted:

class A {};

int main() {
    func(A); // do { std::cout << typeid(A).name() <<std::endl; } while(false);
}

2 Comments

I was tempted to add this to my answer. Imho it should be noted that func is not a function. It is a function-like macro. And the difference is not just the ability to pass a type as parameter. For example it is not possible to get a function pointer.
@463035818_is_not_a_number Indeed. I added a note.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.