我正在开发一个大量使用模板的库,因此我决定将其作为一个仅限标题的库.由于声明和实现在同一个文件中,我现在可以同时执行这两个操作.所以我可以选择这两种风格:
// seperate declaration and implementation
template <typename T>
class Klass {
public:
void do_something();
};
template <typename T>
void Klass<T>::do_something()
{
// do something
}
// or both at the same time
template <typename T>
class Klass {
public:
void do_something()
{
// do something
}
};
Run Code Online (Sandbox Code Playgroud)
我想知道这两个编译器之间是否存在差异.如果不是你会推荐哪一个更好的做法?
如果合并定义和实现,您将在解决交叉/循环依赖关系时遇到问题。
例如:
主页.hpp
#ifndef HOME_HPP
# define HOME_HPP
# include <string>
# include "human.hpp"
template <typename T>
class Home
{
public:
Home()
:
color_("white"),
inhabitant_(this)
{
}
void set_color(const std::string& color)
{
color_ = color;
}
private:
std::string color_;
Human<T> inhabitant_;
};
#endif
Run Code Online (Sandbox Code Playgroud)
人类.hpp
#ifndef HUMAN_HPP
# define HUMAN_HPP
# include <string>
// Cannot include the other header here because it already includes us
//# include "house.hpp"
// So we do a forward declaration:
template<typename T>
class Home;
template <typename T>
class Human
{
public:
Human(Home<T>* house)
:
house_(house)
{
}
void paint(const std::string& color)
{
// Oops, we can't use House in our implementation
// because it is only forward declared
//house_->set_color(color);
}
private:
Home<T>* house_;
};
#endif
Run Code Online (Sandbox Code Playgroud)
您不能使用其他对象。
而如果您将实现拆分到另一个文件中:
主页.hpp
#ifndef HOME_HPP
# define HOME_HPP
# include <string>
# include "human.hpp"
template <typename T>
class Home
{
public:
Home();
void set_color(const std::string& color);
private:
std::string color_;
Human<T> inhabitant_;
};
#endif
Run Code Online (Sandbox Code Playgroud)
主页.ipp
#ifndef HOME_IPP
# define HOME_IPP
# include "home.hpp"
template<typename T>
Home<T>::Home()
:
color_("white"),
inhabitant_(this)
{
}
template<typename T>
void Home<T>::set_color(const std::string& color);
{
color_ = color;
}
#endif
Run Code Online (Sandbox Code Playgroud)
人类.hpp
#ifndef HUMAN_HPP
# define HUMAN_HPP
# include <string>
template<typename T>
class Home;
template <typename T>
class Human
{
public:
Human(Home<T>* house);
void paint(const std::string& color);
private:
Home<T>* house_;
};
#endif
Run Code Online (Sandbox Code Playgroud)
人类.ip
#ifndef HUMAN_IPP
# define HUMAN_IPP
# include "human.hpp"
# include "house.ipp"
template<typename T>
Human<T>::Human(Home<T>* house)
:
house_(house)
{
}
template<typename T>
void Human<T>::paint(const std::string& color)
{
house_->set_color(color);
}
#endif
Run Code Online (Sandbox Code Playgroud)
事情进行得更加顺利