Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to correctly return std::type_info from class method in C++

Tags:

c++

c++11

I have a class template which should be able to return its template type via std::type_info:

template <class factType>
class Belief : public BeliefRoot
{
    /* ... */

    factType m_Fact;

    std::type_info GetBeliefType() const override
    {
        return typeid(m_Fact);
    }
};

But compiling that class gives me the following error:

error C2280: 'type_info::type_info(const type_info &)': attempting to reference a deleted function

Any ideas what I am missing here?

like image 400
Matthias Avatar asked Dec 07 '16 00:12

Matthias


1 Answers

The issue is unrelated to templates, it's caused because type_info is non-copyable. Every type_info object you need is already compiled into your program, and typeid() doesn't make a new one, it returns a const reference to the existing one.

You should make your GetBeliefType function return by const reference:

const std::type_info& GetBeliefType() const override
like image 142
IanPudney Avatar answered Sep 23 '22 06:09

IanPudney