Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Constexpr class function definition linking error

Function is declared in hpp file like this:

class StringProcessor
{
    static constexpr const char* string_process(const char* initial_string, std::size_t string_length, const char* key, std::size_t key_length);
};

And defined in cpp like this:

constexpr const char* StringProcessor:: string_process(const char* initial_string, std::size_t string_length, const char* key, std::size_t key_length)
{
     ...
}

How do I call it, because following line throws Undefined symbols for architecture x86_64: "StringProcessor::string_process(char const*, unsigned long, char const*, unsigned long)", referenced from: _main in main.cpp.o error:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

std::cout << StringProcessor::string_process("Test", 4, "Test", 4) << std::endl;

>Solution :

constexpr functions are implicitly inline and therefore need to be defined in every translation unit where they are used. That usually means that the definition should go into a header shared between the translation units using the function.

Also, in order for constexpr to be of any use, the function needs to be defined before its potential use in a constant expression. Therefore it usually only really makes sense to define it directly on the first declaration, i.e. here in the class definition in the header.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading