4

If I have a file like this, everything works as expected:

#include <filesystem>
#include <iostream>
int main() {
   std::filesystem::path o = "C:\\Windows\\write.exe";
   auto s = o.parent_path();
   std::cout << s << std::endl;
}

However I would like to use a line like this if possible:

filesystem::path o = "C:\\Windows\\write.exe";

I tried this but I get an error:

// using-declaration may not name namespace 'std::filesystem'
using std::filesystem;

and error with this too:

using namespace std::filesystem;
// error: 'filesystem' has not been declared
filesystem::path o = "C:\\Windows\\write.exe";

Is it possible to do what I am attempting?

1 Answer 1

12

You can use a namespace alias like

namespace filesystem = std::filesystem;

Here is a demonstrative program

#include <iostream>

namespace A
{
    namespace B
    {
        int x;
    }
}

int main() 
{
    namespace B = A::B;
    
    B::x = 10;
    
    std::cout << B::x << '\n';
    
    return 0;
}

Its output is

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

1 Comment

Such a precise and simple answer. Nice!

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.