I have a question if there is a way to implement decltype keyword functionality pre c++11.
我有一个问题,如果有一种方法可以实现decltype关键字功能pre c ++ 11。
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
我希望能够在通用MACRO中获得可用于此MyVector的类型T.
#define ALLOCATE_SPACE(VectorRef, numberOfItems) \
{ \
arrayPtr = new decltype(VectorRef)::ElementType[numberOfItems]; \ \
} \
The problem is I can't use c++11 stuff. The perfect solution would be to make it 100% compile time type deduction.
问题是我不能使用c ++ 11的东西。完美的解决方案是使其100%编译时间类型扣除。
Can anyone help me with this issue?
任何人都可以帮我解决这个问题吗?
Best Regards
1 个解决方案
#1
0
This is possible. There is a boost macro accomplishing this magic:
这个有可能。有一个提升宏实现这个魔力:
#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). Live Demo
打印f(浮点)。现场演示
Though, as already pointed out in the comments, you don't need this for your problem. A simple template function would do the job.
虽然,正如评论中已经指出的那样,你不需要这个问题。一个简单的模板功能可以完成这项工作。
#1
0
This is possible. There is a boost macro accomplishing this magic:
这个有可能。有一个提升宏实现这个魔力:
#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). Live Demo
打印f(浮点)。现场演示
Though, as already pointed out in the comments, you don't need this for your problem. A simple template function would do the job.
虽然,正如评论中已经指出的那样,你不需要这个问题。一个简单的模板功能可以完成这项工作。