编译时间类型检查C ++ [英] Compile time type checking C++

查看:96
本文介绍了编译时间类型检查C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个类型列表。然后我使用传递类型列表的模板创建一个类。当我调用类的打印函数有一些类型未指定他们被强制转换。如何在编译时强制实现类型?所以如果我使用一个未列出的类型我得到一个编译器错误。
谢谢。

I have created a type list. I then create a class using a template passing the type list. When I call the print function of the class with a some types not specified they are casted. How can I enforce the exact type at compile time? So if I use an unlisted type I get a compiler error. Thanks.

template <class T, class U>
struct Typelist
{
   typedef T Head;
   typedef U Tail;
};


class NullType
{
};

typedef Typelist<int,Typelist<float,Typelist<char*,NullType> > > UsableTypes;

template<class T>
class MyClass
{
    public:
    void print(T::Head _Value) { std::cout << _Value; }
    void print(T::Tail::Head _Value) { std::cout << _Value; }
    void print(T::Tail::Tail::Head _Value) { std::cout << _Value; }
    private:
};


MyClass<UsableTypes> testclass;

void TestMyClass()
{
    int int_val = 100000;
    float flt_val = 0.1f;
    char* char_val = "Hi";
    short short_val = 10;
    std::string str_val = "Hello";

    testclass.print( int_val ); // OK  8-)
    std::cout << endl;
    testclass.print( flt_val ); // OK  8-)
    std::cout << endl;
    testclass.print( char_val ); // OK  8-)
    std::cout << endl;
    testclass.print( short_val); // this compiles OK and works ???  8-(
    std::cout << endl;
    testclass.print( str_val ); // compile error  8-)
    std::cout << endl;
}

@ Kerrek SB:我以为它会帮助我步骤,这是根据t_list内容,类型和数量的类型创建打印功能。但我努力分离编译时处理和运行时处理。我想做的是为列表中的每个类型创建一个打印功能。因此,如果列表有两种类型,将创建两个打印函数,如果有五个类型,则将为每个类型创建一个打印函数。
当我这样做:

@Kerrek SB: Hi I thought it was going to help me with my next step, which was creating the print function depending on the t_list contents, Types and amounts of types. But I'm struggling to separate compile time processing and runtime processing. What I am trying to do is create a print function for each type in the list. So if the list has two types, two print functions will be created and if there are five types then five print functions will be created one for each type. When I do this:

 typedef Typelist<int,Typelist<float,Typelist<char*,NullType> > > UsableTypes;

 MyClass<UsableTypes> newclass

这是否为列表中的每个类型创建一个MyClass的三个实例,我必须为每种类型创建一个打印功能吗?
我觉得我几乎有所有的块在我心中,但只是不能适应他们在一起。您可以提供的任何帮助将非常感谢。

Does this create three instance of MyClass one for each type in the list or does it create one instance and I have to create a print function for each type? I feel I almost have all the blocks in my mind but just can’t fit them together. Any help you can offer would be gratefully received. Thanks.

推荐答案

添加私人函数模板

template<typename T> void print(T);

这不需要实现。这应该捕获没有显式打印存在的所有类型,并且因为它是私有的,它会给出一个错误消息。

which doesn't need an implementation. This should catch all types for which no explicit print exists, and since it is private, it will give an error message.

这篇关于编译时间类型检查C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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