如何确保模板参数是所需类型的子类型? [英] How to ensure that the template parameter is a subtype of a desired type?

查看:160
本文介绍了如何确保模板参数是所需类型的子类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模板类,我想做的是以下




  1. 确保一个对象只在模板传递的参数是所需类型的子类型

  2. 先前向代码用户传达模板参数必须满足的内容

(1)在某种意义上是自动关注的,如果传递的模板参数不支持该类使用的代码不能编译的一些功能。但是这个错误可能会被检测得相当晚。我希望支票尽早。我还想要完成的是,显而易见的是,传递的模板参数必须从我提供的基本类型派生。



首先,这是误导?如果没有,我该怎么做呢? (最简单的方法,C ++对我来说还是新的)



感谢stackoverflow,你真的加快了我的C ++学习速度。


<给定一些完整的类型 MyBase ,如果,则下面的代码会给出编译时错误: >




<$ c $> pre class =lang-cpp prettyprint-override> #include< boost / type_traits / is_base_of.hpp>
#include< boost / static_assert.hpp>

template< typename T>
class Foo
{
BOOST_STATIC_ASSERT_MSG(
(boost :: is_base_of< MyBase,T> :: value),
T必须是MyBase的后代
);
// Foo实现正常
};

如果您使用带有TR1的C ++ 03编译器,可以使用 std :: tr1 :: is_base_of 而不是 boost :: is_base_of ;如果使用C ++ 11编译器,可以使用 std :: is_base_of ,而不是 boost :: is_base_of static_assert 关键字而不是 BOOST_STATIC_ASSERT_MSG 宏:



< pre class =lang-cpp prettyprint-override> #include< type_traits>

template< typename T>
class Foo
{
static_assert(
std :: is_base_of< MyBase,T> :: value,
T必须是MyBase的后代
);
// Foo实现正常
};

文件连结:

重生 StaticAssert

博斯特 TypeTraits


I have a template class, what I want to do are the following

  1. Make sure that an object is instantiated only if the template parameter passed is a subtype of a desired type
  2. Communicate to the user of the code upfront what is it that the template parameter must satisfy

(1) is sort of taken care automatically in the sense if the template parameter passed does not support some feature that the class uses the code will not compile. But this error may be detected fairly late. I want the checks to be as early as possible. What I also want to accomplish is that it should be obvious rightaway that template parameter that is passed has to be derived from a base type that I provide.

First, is this misguided ? and if not how shall I do this ? (the simplest way please, C++ is still new to me)

Thanks stackoverflow, you have really speeded up my C++ learning rate.

解决方案

Given some complete type MyBase, the following will give a compile-time error if T is not publicly derived from MyBase:

#include <boost/type_traits/is_base_of.hpp>
#include <boost/static_assert.hpp>

template<typename T>
class Foo
{
    BOOST_STATIC_ASSERT_MSG(
        (boost::is_base_of<MyBase, T>::value),
        "T must be a descendant of MyBase"
    );
    // Foo implementation as normal
};

If you're using a C++03 compiler with TR1, you can use std::tr1::is_base_of instead of boost::is_base_of; if you're using a C++11 compiler, you can use std::is_base_of instead of boost::is_base_of and the static_assert keyword instead of the BOOST_STATIC_ASSERT_MSG macro:

#include <type_traits>

template<typename T>
class Foo
{
    static_assert(
        std::is_base_of<MyBase, T>::value, 
        "T must be a descendant of MyBase"
    );
    // Foo implementation as normal
};

Doc links:
Boost.StaticAssert
Boost.TypeTraits

这篇关于如何确保模板参数是所需类型的子类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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