Sorry for this silly question, but is there any way to restrict using
directives to the current file so that they don't propagate to the files that #include
this file?
Restricting `using` directives to the current file
Asked Answered
Perhaps wrapping the code to be included inside its own namespace could achieve the behavior
you want, since name spaces have scope affect.
// FILENAME is the file to be included
namespace FILENAME_NS {
using namespace std;
namespace INNER_NS {
[wrapped code]
}
}
using namespace FILENAME_NS::INNER_NS;
and in some other file
#include <FILENAME>
// std namespace is not visible, only INNER_NS definitions and declarations
...
Hey, cool trick! I think I'll go for this one; Thanks a lot! :) –
Excitability
No, there isn't, which is why you should not use using directives in header files, or any other file that you #include.
To extend this a little bit - the preprocessor (which handles
#include
and other #
-commands) runs before the compiler ever sees the code. The using
directive and any other standard keywords are processed by the compiler. Thus, as far as the compiler is concerned, the header files aren't actually separate files - they're code that happens to be in every file in which they are #include
'd, and thus so are any using
directives you might put in them. –
Fibroin Aah... sad. Thanks for the answer anyway. –
Excitability
@Rahul It's not that bad - you can (and should) of course use
using
directives in your .cpp files. –
Clubhaul Perhaps wrapping the code to be included inside its own namespace could achieve the behavior
you want, since name spaces have scope affect.
// FILENAME is the file to be included
namespace FILENAME_NS {
using namespace std;
namespace INNER_NS {
[wrapped code]
}
}
using namespace FILENAME_NS::INNER_NS;
and in some other file
#include <FILENAME>
// std namespace is not visible, only INNER_NS definitions and declarations
...
Hey, cool trick! I think I'll go for this one; Thanks a lot! :) –
Excitability
Technically you should be able to import them to some internal namespace, and then make the things declared in that visible in the namespace meant for the user.
#ifndef HEADER_HPP
#define HEADER_HPP
#include <string>
namespace my_detail
{
using std::string;
inline string concatenate(const string& a, const string& b) { return a + b; }
}
namespace my_namespace
{
using my_detail::concatenate;
}
#endif
#include <iostream>
#include "header.hpp"
using namespace my_namespace;
int main()
{
std:: //required
string a("Hello "), b("world!");
std::cout << concatenate(a, b) << '\n';
}
Not sure if it is worth the trouble and how well it plays with "argument-dependent lookup".
Too much of trouble indeed. But still a nice work around. So, +1. :) –
Excitability
© 2022 - 2024 — McMap. All rights reserved.
using
directive in headers. – Phylloxerastd::map<std::string, std::tr1::tuple<int, std::string> >
when I can typemap<string, tuple<int, string> >
. – Excitability