Home > Mobile >  Emscripten: how to disable warning: explicit specialization cannot have a storage class
Emscripten: how to disable warning: explicit specialization cannot have a storage class

Time:10-11

I am building my program by using the latest Emscripten compiler. It is based on Clang version 14. Actually it is a small test program which is the following:

#include <iostream>

struct Test {
    template<typename T>
    static inline void Dump(const T& value) {
        std::cout << "[generic] = '" << value << "'\n";
    }

    template<>
    static inline void Dump<std::string>(const std::string& value) {
        std::cout << "[std::string] = '" << value << "'\n";
    }
};

int main() {
    std::string text = "hello";
    Test::Dump(text);
    return 0;
}

When I build it by Emscripten compiler I got the warning:

D:\em_test>emcc a.cpp
a.cpp:10:24: warning: explicit specialization cannot have a storage class
    static inline void Dump<std::string>(const std::string& value) {
    ~~~~~~~            ^
1 warning generated.

If I just remove static keyword from void Dump<std::string> line then there will be no warning. However, this code will cause compilation error in Visual Studio:

D:\em_test\a.cpp(17,11): error C2352: 'Test::Dump': illegal call of non-static member function

But this error is expected and clear. I would like to write a cross-platform program. So, I think I should simple disable this warning in Emscripten. However, I can not find any Emscripten (which is based on clang version 14) command line option for that! And I am asking advice for that.

Actually I tried to use -Wno-static-inline-explicit-instantiation command line option but it did not help:

D:\em_test>emcc -Wno-static-inline-explicit-instantiation a.cpp
a.cpp:10:24: warning: explicit specialization cannot have a storage class
    static inline void Dump<std::string>(const std::string& value) {
    ~~~~~~~            ^
1 warning generated.

However, I see in Clang version 13 user manual description about -Wstatic-inline-explicit-instantiation option but it is about a slightly another warning text. Also it seems that Clang version 14 is not fully released, so, there is no public Clang version 14 user manual.

I can not find any Emscripten or Clang command line option to disable the above warning. Could somebody help me?

CodePudding user response:

Explicit specialization of (both static and non-static) function templates cannot be put into class definitions. Just put it into the enclosing namespace(i.e somewhere after the class):

#include <iostream>

struct Test {
    template <typename T>
    static inline void Dump(const T& value) {
        std::cout << "[generic] = '" << value << "'\n";
    }
};
// Notice Test::
template <>
inline void Test::Dump<std::string>(const std::string& value) {
    std::cout << "[std::string] = '" << value << "'\n";
}

int main() {
    std::string text = "hello";
    Test::Dump(text);
    return 0;
}

inline is never necessary for in-class function definitions but it has different meaning for member variables. inline for out-class is necessary in header files because the explicit specialization is not a template anymore.

  • Related