There seem some strange behaviors for pf.string()
output, where pf
is generated with p.filename()
, where p
is of type boost::filesystem::path
and constructed with char const* or std::string.
Here is the code segment:
#include <boost/filesystem.hpp>
namespace fs = boost::filesystem;
int main(int argc, char **argv) {
fs::path p(argv[0]); // or fs::path p((std::string(argv[0])));
fs::path &&pf = p.filename(); // or fs::path pf = p.filename();
std::string const &name = p.filename().string();
std::cout << "*" << name << "*\n";
std::string const &p_name = pf.string();
std::cout << "*" << p_name << "*\t";
std::cout << "*" << name << "*\n";
std::string s_name = p.filename().string();
std::cout << "*" << s_name << "*\t";
std::cout << "*" << name << "*\n";
return 0;
}
The argv[0]
here is fs.out
and the output of the executable(compiled with clang3.4
/ gcc4.9
with -O3
/-O0
) is:
**
*fs.out* **
*fs.out* *fs.out*
The boost version I used is 1.55 from Debian jessie(testing) package.
My questions:
- Why
name
is empty in the first 2 lines? - Why
p_name
is not empty butname
is empty at Line 2? - Why this program has the correct(?) output at Line 3 although it seems that there is no relationship between
s_name
andname
?