What are the differences between typedef and using?

前端 未结 3 818
隐瞒了意图╮
隐瞒了意图╮ 2021-01-01 21:56

What are the differences between using

typedef Some::Nested::Namespace::TypeName TypeName;

or

using Some::Nested::Namespace         


        
3条回答
  •  囚心锁ツ
    2021-01-01 22:31

    Using just brings declaration into the local scope, while typedef introduces a typedef-name. One difference between them is elaborated type specifiers, e.g.:

    
    namespace n
    {
      class foo
      {
      };
    }
    
    typedef n::foo n_foo;
    
    using n::foo;
    
    int main()
    {
      class foo f1; // ok, declares f1 variable of type n::foo.
      class n_foo f2; // error, typedef-name can't be used in elaborated-type-specifier.
    }
    
    

提交回复
热议问题