代码之家  ›  专栏  ›  技术社区  ›  not-a-user

是否可以在不使用decltype的情况下推断先前定义的外部变量的类型

  •  3
  • not-a-user  · 技术社区  · 6 年前
    // header
    
    int extern has_a_type; // (1) extern declaration
    
    // implementation
    
    decltype(has_a_type)   // (2) unnecessarily verbose type inference code
    has_a_type;            // (3) definition
    

    我知道我可以用 decltype 脱模 强制我两次(2)写出变量的名称(可能是完全限定的和长的)。

    我怎样才能避免写两遍呢?类似于 auto has_a_type;

    1 回复  |  直到 6 年前
        1
  •  3
  •   Bathsheba    6 年前

    int extern has_a_type;
    auto has_a_type;
    

    尽管它很容易处理。结果可能是

    decltype(auto) has_a_type;
    

    为了从初始值设定项中消除类型推导的歧义是必要的,然后,不幸的是,我们离重复性没有太远 decltype(has_a_type) 已经有了。