Is it possible to declare a const vector in a header file?

Below is some simplified code from a header file where free functions are declared but not defined and a vector is declared and defined.

The cpp file contains the implementation of free functions.

I was wondering if there is a way to declare the vector in the header file and put the definition in the cpp file.

// my-file.h

namespace MyNamespace
{
    bool foo(const std::string& name, const std::string& value);
    void bar(const std::string& name, const std::string& value);

    const std::vector<std::function<void(const std::string&, const std::string&)>> m_myVector
    {
        foo,
        bar,
        [](const std::string& name, const std::string& value)
        {
            // do some stuff
        }
    };

} // MyNamespace

      

+3


source to share


1 answer


You can declare a const variable in your header like:



extern
const std::vector<std::function<void(const std::string&, const std::string&)>> m_myVector;

      

+4


source







All Articles