简体   繁体   English

在编译时获取表达式的类型

[英]Get type of expression at compile time

While programming with the auto keyword it would sometimes be convenient to know the type that is used by the compiler at compile time. 在使用auto关键字进行编程时,有时会很方便在编译时知道编译器使用的类型。 It does not matter if the compilation aborts where I need to know the type. 编译是否中止我需要知道类型的位置都没关系。 Simple example: 简单的例子:

std::vector< int > s{1, 2, 3}; 

for (auto elem : s) {
    elem = 5;
}   

for (auto elem : s) {
    std::cout << elem << std::endl;
}   

will print 将打印

1
2
3

because elem is of type int , not int& . 因为elem是int类型,而不是int&类型。 It would be nice to try compile the code and get the type of elem to catch such mistakes early. 尝试编译代码并获得elem的类型以尽早发现此类错误将是很好的。

Classical way is to declare template structure without definition: 经典方法是声明模板结构而无需定义:

template <typename> struct Debug;

and then use it: 然后使用它:

template struct Debug<std::string>;

or 要么

for (auto elem : s) {
    Debug<decltype(elem)>{};

    elem = 5;
}

Message error looks like 消息错误看起来像

error: explicit instantiation of 'struct Debug<std::__cxx11::basic_string<char> >' before definition of template
 template struct Debug<std::string>;
                 ^~~~~~~~~~~~~~~~~~

error: invalid use of incomplete type 'struct Debug<int>'
         Debug<decltype(e)>{};

Demo 演示版

BTW, now some IDEs show the type when mouse is over auto or the variable. 顺便说一句,现在某些IDE在鼠标悬停在auto或变量上方时显示类型。

Actually I just found an answer to my own question: 实际上,我只是找到了自己的问题的答案:

template<typename T>
void show_type_abort_helper()
{
    return __PRETTY_FUNCTION__;
}

#define show_type_abort(x) show_type_abort_helper< decltype(x) >()

Usage: 用法:

std::vector< int > s{1, 2, 3};

for (auto elem : s) {
    show_type_abort(elem);
    elem = 5;
}

Produces the following error message with g++ (version 6.1.1): 使用g++ (版本6.1.1)产生以下错误消息:

$ g++ test.cpp
test.cpp: In instantiation of ‘void show_type_abort_helper() [with T = int]’:
                                                                       ^^^
test.cpp:17:9:   required from here
test.cpp:7:12: error: return-statement with a value, in function returning 'void' [-fpermissive]
    return __PRETTY_FUNCTION__;

Check the output at T = int to see the compiler uses int as type. 检查T = int的输出,以查看编译器使用int作为类型。 This seems to work with clang as well: 这似乎也适用于clang:

$ clang++-3.8 -std=c++11 test.cpp
test.cpp:7:5: error: void function 'show_type_abort_helper' should not return a value [-Wreturn-type]
    return __PRETTY_FUNCTION__;
    ^      ~~~~~~~~~~~~~~~~~~~
test.cpp:17:9: note: in instantiation of function template specialization 'show_type_abort_helper<int>' requested here
                                                                                                  ^^^
        show_type_abort(elem);

Changing to for (const auto& elem : s) gives 更改为for (const auto& elem : s)得到

with T = const int&
         ^^^^^^^^^^

or 要么

show_type_abort_helper<const int &>
                       ^^^^^^^^^^^

So it seems I can find out the type at compile time and abort. 所以看来我可以在编译时找出类型并中止。 This just came in handy for a very complex type that consisted of several typedefs and template parameters where I just could not see what was going on. 这对于非常复杂的类型非常有用,它由几个typedef和模板参数组成,而我看不到发生了什么。

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

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