c++ – Can a using-declaration appear at block/function scope? – Education Career Blog

My question is pretty much the title.
Example

#include <iostream>

int main()
{
   using std::cout; //legal?
   { 
      using std::cin; //legal?
   }
}

,

Yes, it’s legal. In fact, it’s preferred over doing it at file scope…

,

  • 7.3.3 The using declaration

A using-declaration introduces a name into the declarative region in which the using-declaration appears.

And, since someone asked in a comment about using namespace:

  • 7.3.4 Using directive

A using-directive shall not appear in class scope, but may appear in namespace scope or in block scope.

,

Why didn’t you test it? It works alright.

Leave a Comment