2

My question is pretty much the title. Example

#include <iostream>

int main()
{
   using std::cout; //legal?
   { 
      using std::cin; //legal?
   }
}
5
  • No problem. Backticks don't work to format code over multiple lines. Multiline code blocks must be indented by 4 spaces. Commented Oct 14, 2010 at 15:32
  • s/iostrean/iostream Can't edit myself yet. Commented Oct 14, 2010 at 15:38
  • What's the reason for downvoting the question? Commented Oct 14, 2010 at 15:41
  • @Daniel. No, it's not. There are many things that would compile on my compiler (MSVC9.0) which are NOT legal. I don't know why I started doubting about this so I asked here. Commented Oct 14, 2010 at 19:29
  • So why don't try to use it. Add cin inside of scope where using cin, also add cin (without the std::) outside scope. Also can you give an "example" of illegal thing that compiles? Commented Oct 15, 2010 at 6:40

3 Answers 3

5

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

Sign up to request clarification or add additional context in comments.

2 Comments

I am sorry but I haven't heard of such a thing as a file scope
Does the same apply to using namespace?
4
  • 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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.