简体   繁体   English

如何实现decltype功能-C ++ 11之前的编译时间

[英]How to implement decltype functionality - pre c++11, compile time

I have a question if there is a way to implement decltype keyword functionality pre c++11. 我有一个问题,是否可以在c ++ 11之前实现decltype关键字功能。

I have a simplified class of vector 我有一类简化的向量

template <class T>
struct MyVector {

    typedef T ElementType;

    T* arrayPtr;
    uint64_t numberOfElements;
};

I want to be able to get the type T in a universal MACRO that will be usable with this MyVector 我希望能够在可用于此MyVector的通用MACRO中获得T型

#define ALLOCATE_SPACE(VectorRef, numberOfItems) \
{ \
    arrayPtr = new decltype(VectorRef)::ElementType[numberOfItems]; \ \
} \

The problem is I can't use c++11 stuff. 问题是我不能使用c ++ 11的东西。 The perfect solution would be to make it 100% compile time type deduction. 完美的解决方案是使其100%编译时类型扣除。

Can anyone help me with this issue? 谁能帮我解决这个问题?

Best Regards 最好的祝福

This is possible. 这个有可能。 There is a boost macro accomplishing this magic: 有一个Boost宏可以实现以下功能:

#include <boost/typeof/typeof.hpp>
#include <iostream>
#include <typeinfo>

int main() {
    int a; float b;
    BOOST_TYPEOF(a+b) c = a+b;
    std::cout << typeid(c).name() << std::endl;
}

prints f (float). 打印f (浮点)。 Live Demo 现场演示

Though, as already pointed out in the comments, you don't need this for your problem. 但是,正如评论中已经指出的那样,您并不需要此来解决问题。 A simple template function would do the job. 一个简单的模板功能就可以完成这项工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM