-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;.

yingzi
  • 25
  • 5
  • Do you mean that you want to call the function with an object or with the mangled name of a type? – Ted Lyngmo Aug 26 '22 at 08:29
  • 1
    You probably want a template. Do you know what templates are in C++? If not you should research that topic. – Lukas-T Aug 26 '22 at 08:34
  • 1
    What @churill said. Is [this](https://godbolt.org/z/39Kq6WdcE) what you want? – Ted Lyngmo Aug 26 '22 at 08:35
  • You can't define a function that takes a type as a parameter. – molbdnilo Aug 26 '22 at 08: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. – user17732522 Aug 26 '22 at 08:42
  • @TedLyngmo @churill 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. @molbdnilo Yes, I want use a type as a parameter. If type can not be used as parameter, why `typeid` can accept the type as a parameter? @user17732522 I have provided an example about how I want to use this `func`. – yingzi Aug 26 '22 at 09:07
  • `typeid` is built into the language. You also can't implement `sizeof`, `decltype`, or `declval` as functions. – molbdnilo Aug 26 '22 at 09:09
  • @yingzi then you're out of luck... Why don't you want to use templates? – Ted Lyngmo Aug 26 '22 at 09:09
  • @TedLyngmo I am just interested in `typeid`. Actually, template is enough for my project. – yingzi Aug 26 '22 at 09:15

2 Answers2

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);
463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • What do you mean by `typeid is an operator`? How to implement myself `func` operator? Could you please provide an example? – yingzi Aug 26 '22 at 09:11
  • @yingzi You can overload some existing operators but you can't invent your own operators or overload `typeid`. – Ted Lyngmo Aug 26 '22 at 09:13
  • @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 – 463035818_is_not_an_ai Aug 26 '22 at 09:15
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);
}
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
  • 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_an_ai Aug 26 '22 at 09:28
  • @463035818_is_not_a_number Indeed. I added a note. – Ted Lyngmo Aug 26 '22 at 09:30