简体   繁体   English

关键字typeof in c ++ 11

[英]Keyword typeof in c++11

I know: 我知道:

  • typeof is a gcc extension and is not part of the C++ standard. typeof是gcc扩展,不是C ++标准的一部分。

Questions: 问题:

  1. Is the word typeof deprecated in C++11? 在C ++ 11中是否弃用了typeof in other words, is it allowed to still use it as a gcc extension when using C++11? 换句话说,在使用C ++ 11时,是否允许它仍然用作gcc扩展名?
  2. Is it correct to say that replacing every typeof with decltype yields the same behaviour of the code? 它是正确的说,替换每个typeofdecltype产生的代码相同的行为?
  3. Assume I have template<typename T> class wrapper . 假设我有template<typename T> class wrapper What is the best way to declare wrapper_some_field such that it is equivalent to: Wrapper<typeof(some_field)> wrapper_some_field 声明wrapper_some_field的最佳方法是什么,它等效于: Wrapper<typeof(some_field)> wrapper_some_field

Is the word typeof deprecated in C++11? 在C ++ 11中是否弃用了typeof in other words, is it allowed to still use it as a gcc extension when using C++11? 换句话说,在使用C ++ 11时,是否允许它仍然用作gcc扩展名?

It's not deprecated . 它没有被弃用 It never existed as a keyword. 它从未作为关键字存在。 gcc suggests that if you compile with -std=c++** that you instead use __typeof__ . gcc 建议如果使用-std=c++**编译,则改为使用__typeof__

Is it correct to say that replacing every typeof with decltype yields the same behaviour of the code? 它是正确的说,替换每个typeofdecltype产生的代码相同的行为?

No. For example, given: 不,例如,给定:

int& foo();

decltype(foo()) is int& but __typeof__(foo()) is int . decltype(foo())int&但是__typeof__(foo())int

Assume I have template<typename T> class wrapper . 假设我有template<typename T> class wrapper [...] [...]

You could write wrapper<std::remove_reference_t<decltype(some_field)>> wrap{some_field} , but it'd be cleaner to write a construction function template: 您可以编写wrapper<std::remove_reference_t<decltype(some_field)>> wrap{some_field} ,但编写构造函数模板会更清晰:

template <class T> wrapper<T> make_wrapper(T const& val) { return {val}; }
auto wrap = make_wrapper(some_field);

Or, with forwarding: 或者,转发:

template <class T>
wrapper<std::decay_t<T>> make_wrapper(T&& val) {
    return {std::forward<T>(val)};
}

Although in C++17 you wouldn't do this at all and would just use class template argument deduction: 虽然在C ++ 17中你根本不会这样做,只会使用类模板参数推导:

template <class T> struct wrapper { T t; };
template <class T> wrapper(T) -> wrapper<T>;
auto wrap = wrapper{42}; // wrap is a wrapper<int>

And in C++20, you won't even need the deduction guide. 在C ++ 20中,您甚至不需要演绎指南。

#define typeof(...) std::remove_reference_t<decltype(__VA_ARGS__)>;

However, if you want to create storage for a type T , the way to do it in C++11 is to use std::decay_t , or in some situations write your own extension that stores C-style arrays into a std::array . 但是,如果要为类型T创建存储,在C ++ 11中执行此操作的方法是使用std::decay_t ,或者在某些情况下编写自己的扩展,将C样式数组存储到std::array

Wrapper<std::decay_t<T>> wrapper_some_field;

if you want to pass Wrapper a type suitable for storing inside of it. 如果你想通过Wrapper一个适合存储在其中的类型。

decay removes references, converts functions to pointers-to-functions, and arrays of T to pointers-to-T, and removes top-level const and volatile after that. decay删除引用,将函数转换为指向函数的指针,将T数组转换为指向T的指针,然后删除顶级constvolatile These are operations similar to what happens when you pass things to a function as part of the "decay-to-pointer/value" operations. 这些操作类似于将事物作为“衰减到指针/值”操作的一部分传递给函数时发生的操作。

The result is a type "suitable for storage". 结果是“适合存储”的类型。 As noted, I'd prefer that a int[4] decay to a std::array<int,4> but you cannot have everything . 如上所述,我更喜欢int[4]衰变为std::array<int,4>但你不能拥有一切

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

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