如何在C ++中显式实例化模板构造函数? [英] How to explicit instantiate template constructor in C++?
本文介绍了如何在C ++中显式实例化模板构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当前我有以下课程
//AABB.h
class AABB {
public:
template <class T>
AABB(const std::vector<T>& verties);
template <class T>
AABB(const std::vector<int>& indicies, const std::vector<T>& verties);
template <class T>
AABB(const std::vector<int>::const_iterator begin,
const std::vector<int>::const_iterator end,
const std::vector<T>& verties);
AABB();
AABB(const vec3 min, const vec3 max);
AABB(const AABB& other);
const vec3& min_p() const { return m_min; }
const vec3& max_p() const { return m_max; }
vec3 center();
float volume();
float surface_area();
bool inside(vec3 point);
float intersect(const Ray& ray);
private:
vec3 m_min;
vec3 m_max;
};
有三个 AABB
构造函数的模板。有没有一种方法可以声明使用显式实例化?像下面这样?
There are three AABB
constructors that are templated. Is there a possible way I could declare use explicit instantiation? Like the following?
// AABB.cpp
template AABB::AABB<Triangle>(const idx_t, const idx_t, const vector<Triangle>&);
给予
// AABB.cpp
using idx_t = std::vector<int>::const_iterator;
当前来自编译器的错误是
Currently the error from the compiler is
/Users/DarwinSenior/Desktop/cs419/tracer2/src/AABB.cpp:7:16: error: qualified
reference to 'AABB' is a constructor name rather than a type wherever a
constructor can be declared
template AABB::AABB<Triangle>(const idx_t, const idx_t,
^
推荐答案
您快到了,问题在于最后一个参数必须是模板声明中的引用:
You're almost there, the problem is the last argument must be a reference as it's in the template declaration:
template AABB::AABB<Triangle>(const idx_t, const idx_t, const vector<Triangle>&);
// ^^^
此外,正如TC指出的那样,您可以删除< ; Triangle>
,因为它可以从参数的类型中推断出来:
Also, as T.C pointed out, you can remove <Triangle>
because it can be inferred from the type of the argument:
template AABB::AABB(const idx_t, const idx_t, const vector<Triangle>&);
这篇关于如何在C ++中显式实例化模板构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文