小编use*_*595的帖子

如何专门化模板函数以使其接受传递char数组作为参数?

// problem.cpp:
#include <string>

template<typename T> void func(const T & v);

int main() {
        int i;
        float f;
        char * cp;
        char ca[4];

        func(i);
        func(f);
        func(cp);
        func(std::string("std::string"));
        func(ca);
        func("string_literal");

        return 0;
}

// problem2.cpp
#include <string>

template<typename T> void func(const T & v);

// undefined reference to `void func<int>(int const&)'
template<> void func<int>(const int & v) { }

// undefined reference to `void func<float>(float const&)'
template<> void func<float>(const float & v) { }

// undefined reference to `void func<char*>(char* const&)' …
Run Code Online (Sandbox Code Playgroud)

c++ templates

5
推荐指数
1
解决办法
4201
查看次数

标签 统计

c++ ×1

templates ×1