在链接期间未定义的对部分专用模板类功能的引用 [英] Undefined reference to partial specialized template class function during link time

查看:94
本文介绍了在链接期间未定义的对部分专用模板类功能的引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我对功能模板的部分专业化有疑问.我选择此处描述的解决方案:问题

现在我有这个了

#include <vector>
#include <iostream>

template <typename T> struct helper {
    static void print(T value) { std::cout << value; }
};
template <typename T> struct helper<std::vector<T>> {
    static void print(std::vector<T> const &value) { }
};

template <typename T>
void print (T const &value) {
    // Just delegate.
    helper<T>::print (value);
}


int main () {
    print (5);
    std::vector<int> v;
    print (v);
}

但是我想要这样的布局:

helper.hpp

#include <vector>
#include <iostream>

template <typename T> struct helper {
    static void print(T value) { std::cout << value; }
};

vector_helper.cpp

#include <vector>
#include <iostream>

template <typename T> struct helper<std::vector<T>> {
    static void print(std::vector<T> const &value) { }
};

print.hpp

#include "helper.hpp"

template <typename T>
void print (T const &value) {
    // Just delegate.
    helper<T>::print (value);
}

main.cpp

#include "print.hpp"

int main () {
    print (5);
    std::vector<int> v;
    print (v);
}

像这样编译:

g++ main.cpp vector_helper.cpp

问题在于MinGW正在产生链接时错误:对helper<vector<...>>::print(vector<...>)

的未定义引用

当我添加行时:

#include "vector_helper.cpp"

int main() {...}之前的

,它可以正常编译并且可以工作.我要如何解决它,因为我想在通过g ++命令链接的文件中添加类特化.

解决方案

这些模板类不能拆分为单独的对象文件,并且完全不专门.如果您查看诸如vector之类的标准模板,则将看到所有内容都在单个头文件中.

如果要隐藏这样的模板实现,则必须将其实例化为一种或多种特定类型.您可以通过粘贴类似

的方法来完成此操作

template class helper<std::vector<int>>;

如果我没记错的话,请按vector_helper.cpp结尾的

.但是最好不要将所有模板都放在标题中.

So I had an problem with partial specialization of function templates. I choose the solution described here: Question

Now I have this:

#include <vector>
#include <iostream>

template <typename T> struct helper {
    static void print(T value) { std::cout << value; }
};
template <typename T> struct helper<std::vector<T>> {
    static void print(std::vector<T> const &value) { }
};

template <typename T>
void print (T const &value) {
    // Just delegate.
    helper<T>::print (value);
}


int main () {
    print (5);
    std::vector<int> v;
    print (v);
}

But I want to have this layout:

helper.hpp

#include <vector>
#include <iostream>

template <typename T> struct helper {
    static void print(T value) { std::cout << value; }
};

vector_helper.cpp

#include <vector>
#include <iostream>

template <typename T> struct helper<std::vector<T>> {
    static void print(std::vector<T> const &value) { }
};

print.hpp

#include "helper.hpp"

template <typename T>
void print (T const &value) {
    // Just delegate.
    helper<T>::print (value);
}

main.cpp

#include "print.hpp"

int main () {
    print (5);
    std::vector<int> v;
    print (v);
}

Compiled like this:

g++ main.cpp vector_helper.cpp

The problem is that MinGW is producing linking-time erros: undefined reference to helper<vector<...>>::print(vector<...>)

When I add the line:

#include "vector_helper.cpp"

before int main() {...}, it compiles fine and also works. How can I solve it, because I want to add the class specialization in the file linked by the g++ command.

解决方案

Those template classes can't be split into separate object files and remain wholly unspecialised. If you look at the standard templates like vector, you'll see that everything is in a single headerfile for this reason.

If you want to hide the implementation of your templates like that, you have to force instantiation of them to one or more specific types. You might do this by sticking something like

template class helper<std::vector<int>>;

at the end of vector_helper.cpp, if I recall correctly. But you're best off keeping all your templates in headers.

这篇关于在链接期间未定义的对部分专用模板类功能的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆