Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using-declaration may not name namespace

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?

like image 874
Zombo Avatar asked Sep 19 '25 14:09

Zombo


1 Answers

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
like image 65
Vlad from Moscow Avatar answered Sep 21 '25 05:09

Vlad from Moscow