在一个名为
Foo
的类中,我有以下模板函数:class Foo
{
public:
template <typename T>
static typename boost::enable_if<boost::is_abstract<T>, T*>::type allocate();
template <typename T>
static typename boost::disable_if<boost::is_abstract<T>, T*>::type allocate();
};
有两个声明,但是对于用户来说只有一个函数。
在Doxygen中记录这种声明的通常方法是什么?
想法 #1:
class Foo
{
public:
/** \brief This function throws a runtime error */
template <typename T>
static typename boost::enable_if<boost::is_abstract<T>, T*>::type allocate();
/** \brief This function allocates an instance of the given type */
template <typename T>
static typename boost::disable_if<boost::is_abstract<T>, T*>::type allocate();
};
点子 #2:
class Foo
{
public:
/** \brief This function allocates an instance of the given type if not abstract, throws an exception instead */
template <typename T>
static typename boost::enable_if<boost::is_abstract<T>, T*>::type allocate();
template <typename T>
static typename boost::disable_if<boost::is_abstract<T>, T*>::type allocate();
};
BOOST_STATIC_ASSERT_MSG(!boost::is_abstract<T>::value, "T must be concrete")
。如果不满足T
的要求,则会生成编译错误,这比运行时异常更可取。在C++11中的等效语句是static_assert(!std::is_abstract<T>::value, "T must be concrete")
,而在C++17中则为static_assert(!std::is_abstract_v<T>, "T must be concrete")
。 - Emile Cormierallocate
调用new T
。在执行过程中,其他函数永远不会被调用(在之前进行了一些检查),但编译器并不知道它,并想要编写该分支。 - Caduchon