Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/128.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
visualc中的类型推理&x2B+;2008 微软Visual C++ 2008中有一些厂商特有的类型推理机制,类似于标准化的自动或 DeCype < /C> > C++ 0x? < P>不,没有类似的,标准的,也没有厂商特定的,也没有ADDN的。您必须升级到VS2010,它实现了自动。_C++_Visual C++_C++11_Type Inference_Decltype - Fatal编程技术网

visualc中的类型推理&x2B+;2008 微软Visual C++ 2008中有一些厂商特有的类型推理机制,类似于标准化的自动或 DeCype < /C> > C++ 0x? < P>不,没有类似的,标准的,也没有厂商特定的,也没有ADDN的。您必须升级到VS2010,它实现了自动。

visualc中的类型推理&x2B+;2008 微软Visual C++ 2008中有一些厂商特有的类型推理机制,类似于标准化的自动或 DeCype < /C> > C++ 0x? < P>不,没有类似的,标准的,也没有厂商特定的,也没有ADDN的。您必须升级到VS2010,它实现了自动。,c++,visual-c++,c++11,type-inference,decltype,C++,Visual C++,C++11,Type Inference,Decltype,引用Arkadiy Vertleyb的一篇文章: 上帝只知道还能找到什么 如果愿意的话,在这个编译器中 深入挖掘 例如,来自RSDN的Igor’Chesnokov (俄罗斯软件开发网) 找到了实现typeof()的方法 这不需要注册, 而且可能有编译时间 本机类型的性能 怎么做?显然,有些奇怪的“特征” VisualC++允许他用 模板主体在安装时 实例化,当附加上下文时 可用,因此“注册” 课程在飞行中,在 使用typeof() Microsoft特定的“bugfeatures”包括 通常不在

引用Arkadiy Vertleyb的一篇文章:

上帝只知道还能找到什么 如果愿意的话,在这个编译器中 深入挖掘

例如,来自RSDN的Igor’Chesnokov (俄罗斯软件开发网) 找到了实现typeof()的方法 这不需要注册, 而且可能有编译时间 本机类型的性能

怎么做?显然,有些奇怪的“特征” VisualC++允许他用 模板主体在安装时 实例化,当附加上下文时 可用,因此“注册” 课程在飞行中,在 使用typeof()

Microsoft特定的“bugfeatures”包括 通常不在我的工作范围内 兴趣。但我确实意识到, 在Microsoft编译器上,这可能会 那你看起来更迷人 佩德和我实施的任何事情。 即使我意识到 我想这可能是一场激烈的比赛 更不用说我会觉得很难过 这是:

并从上面引用的链接引用的页面中引用Igor Chesnokov的代码:

// type_of() evil implementation for VC7
//
// (c) Chez
// mailto:chezu@pisem.net

#include "stdafx.h"

// This file contains:
// 1) type_id(type)
// 2) var_type_id(expersssion)
// 3) type_of(expression)

// IMPLEMENTATION
template<int ID>
class CTypeRegRoot
{
public:
    class id2type;
};

template<typename T, int ID>
class CTypeReg : public CTypeRegRoot<ID>
{
public:
    class CTypeRegRoot<ID>::id2type // This uses nice VC6-VC7 bugfeature
    {
    public:
        typedef T Type;
    };

    typedef void Dummy;
};

template<int N>
class CCounter;

// TUnused is required to force compiler to recompile CCountOf class
template<typename TUnused, int NTested = 0>
class CCountOf
{
public:
    enum
    {
        __if_exists(CCounter<NTested>) { count = CCountOf<TUnused, NTested + 1>::count }
        __if_not_exists(CCounter<NTested>) { count = NTested }
    };
};

template<class TTypeReg, class TUnused, int NValue> // Helper class
class CProvideCounterValue
{
public:
    enum { value = NValue };
};

// type_id
#define type_id(type) \
    (CProvideCounterValue< \
        /*register TYPE--ID*/ typename CTypeReg<type, CCountOf<type >::count>::Dummy, \
        /*increment compile-time Counter*/ CCounter<CCountOf<type >::count>, \
        /*pass value of Counter*/CCountOf<type >::count \
     >::value)

// Lets type_id() be > than 0
class __Increment_type_id { enum { value = type_id(__Increment_type_id) }; };

template<int NSize>
class sized
{
private:
    char m_pad[NSize];
};

template<typename T>
typename sized<type_id(T)> VarTypeID(T&);
template<typename T>
typename sized<type_id(const T)> VarTypeID(const T&);
template<typename T>
typename sized<type_id(volatile  T)> VarTypeID(volatile T&);
template<typename T>
typename sized<type_id(const volatile T)> VarTypeID(const volatile T&);

// Unfortunatelly, var_type_id() does not recognize references
#define var_type_id(var) \
    (sizeof(VarTypeID(var)))

// type_of
#define type_of(expression) \
    /* This uses nice VC6-VC7 bugfeature */ \
    CTypeRegRoot<var_type_id(expression)>::id2type::Type

// auto_operator
#define auto_operator(arg1, arg2, op) \
    type_of(instance(arg1) op instance(arg2)) operator op


// TEST    
class A
{
public:
    friend static const char* operator +(const A& a, const A& b)
    {
        return "chijik-pijik";
    }
};

template<typename T>
class Plus
{
public:
    friend static type_of(T() + T()) operator +(const Plus<T>& a, const Plus<T>& b)
    {
        return a.m + b.m;
    }

    T m;
};

int _tmain(int argc, _TCHAR* argv[])
{
    Plus<A> a1, a2;
    const char* x = a1 + a2;

    return 0;
}
//VC7的邪恶实现类型
//
//(c)Chez
//邮寄地址:chezu@pisem.net
#包括“stdafx.h”
//此文件包含:
//1)类型标识(类型)
//2)变量类型id(支出)
//3)类型(表达式)
//实施
模板
类CTypeRegRoot
{
公众:
ID2类;
};
模板
类CTypeReg:publiccTypeRegroot
{
公众:
类CTypeRegRoot::id2type//这使用了很好的VC6-VC7功能
{
公众:
T型;
};
typedef无效假人;
};
模板
班级会计;
//强制编译器重新编译CCountOf类需要使用TUnused
模板
C类
{
公众:
枚举
{
__如果_存在(CCounter){count=CCountOf::count}
__如果不存在,则存在(计数){count=NTested}
};
};
模板//助手类
类CProvideCounterValue
{
公众:
枚举{value=NValue};
};
//类型识别码
#定义类型\u id(类型)\
(CProvideCounterValue<\
/*寄存器类型--ID*/typename CTypeReg::Dummy\
/*增量编译时计数器*/CCounter\
/*通过计数器的值*/ccuntof::count\
>::价值)
//让类型_id()大于0
类uuu Increment_type_id{enum{value=type_id(uuu Increment_type_id)};};
模板
班级规模
{
私人:
字符m_pad[NSize];
};
模板
typename大小的VarTypeID(T&);
模板
typename大小的VarTypeID(const T&);
模板
typename大小的VarTypeID(volatile T&);
模板
typename大小的VarTypeID(const volatile T&);
//不幸的是,var_type_id()无法识别引用
#定义变量类型变量id(var)\
(sizeof(VarTypeID(var)))
//类型
#定义(表达式)的类型\
/*这使用了漂亮的VC6-VC7功能*/\
CTypeRegRoot::id2type::Type
//自动操作员
#定义自动_运算符(arg1、arg2、op)\
(实例(arg1)op实例(arg2))运算符op的类型
//试验
甲级
{
公众:
友元静态常量字符*运算符+(常量A和A,常量A和b)
{
返回“chijik pijik”;
}
};
模板
班级加号
{
公众:
(T()+T())运算符+(常数加和a、常数加和b)的友元静态类型
{
返回a.m+b.m;
}
T-m;
};
int _tmain(int argc,_TCHAR*argv[]
{
加上a1,a2;
常量字符*x=a1+a2;
返回0;
}
现在我还没有尝试过这段代码,而且,由于它使用了特定于编译器的东西,请注意它是针对MSVC 7.x的。因此,它可能适用于或不适用于更高版本。希望如此


干杯&hth.,

使用BOOST。或者,如果您不想在所有BOOST中跋涉,这里有一个可以与Visual Studio 2008一起使用的片段(但可能没有其他版本):


当然,使用这个时候,请参阅Boost许可证。

如果你现在需要Visual C++ 2010编译器,你可以下载。+ 1:你必须升级到VS2010,以自动或解密或等同于-没有GCC风格的类型()。特快版当然是免费的,但是其他的不是。谢谢,但是已经5年了。我不再使用Visual Studio 2008;)
namespace typeid_detail {

template <int N>
struct encode_counter : encode_counter<N - 1> {};

template <>
struct encode_counter<0> {};

char (*encode_index(...))[5];
// need to default to a larger value than 4, as due to MSVC's ETI errors. (sizeof(int) = 4)

struct msvc_extract_type_default_param {};

template <typename ID, typename T = msvc_extract_type_default_param>
struct msvc_extract_type;

template <typename ID>
struct msvc_extract_type<ID, msvc_extract_type_default_param> {
    template <bool>
    struct id2type_impl;

    typedef id2type_impl<true> id2type;
};

template <typename ID, typename T>
struct msvc_extract_type : msvc_extract_type<ID,msvc_extract_type_default_param> {
    template <>
    struct id2type_impl<true> { // VC8.0 specific bugfeature
        typedef T type;
    };

    template <bool>
    struct id2type_impl;

    typedef id2type_impl<true> id2type;
};

template <typename T, typename ID>
struct msvc_register_type : msvc_extract_type<ID, T> {
};

template <int i>
struct int_ {
    enum { value = i };
};

template <int ID>
struct msvc_typeid_wrapper {
    typedef typename msvc_extract_type<int_<ID> >::id2type id2type;
    typedef typename id2type::type type;
};

template <>
struct msvc_typeid_wrapper<1> {
    typedef msvc_typeid_wrapper<1> type;
};
// workaround for ETI-bug for VC6 and VC7

template <>
struct msvc_typeid_wrapper<4> {
    typedef msvc_typeid_wrapper<4> type;
};
// workaround for ETI-bug for VC7.1

#define TYPEOF_INDEX(T) (sizeof(*encode_index((encode_counter<405/*1005*/>*)0))) // this needs to be lower for VS 2008, otherwise causes too deep templates
#define TYPEOF_NEXT_INDEX(next) friend char (*encode_index(encode_counter<next>*))[next];

template <typename T>
struct encode_type {
    static const unsigned value = TYPEOF_INDEX(T);
    // get the next available compile time constants index

    typedef typename msvc_register_type<T,int_<value> >::id2type type;
    // instantiate the template

    static const unsigned next = value + 1;
    // set the next compile time constants index

    TYPEOF_NEXT_INDEX(next);
    // increment the compile time constant (only needed when extensions are not active)
};

template <class T>
struct sizer {
    typedef char(*type)[encode_type<T>::value];
};

template <typename T> typename sizer<T>::type encode_start(T const&);
// a function that converts a value to size-encoded type (not implemented, only needed for type inference)

template <typename Organizer, typename T>
msvc_register_type<T, Organizer> typeof_register_type(const T&, Organizer* = 0);

} // ~typeid_detail

#define type_of(expr) \
    typeid_detail::msvc_typeid_wrapper<sizeof(*typeid_detail::encode_start(expr))>::type
int x = 123;
float y = 456.0f;
typedef type_of(x+y) int_plus_float;
int_plus_float value = x + y;