Can I use the ftw-function for class methods in C++?
I would like to use the ftw-function to recursivly traverse a filesystem structure. Additionally, the method shall be used inside of a class. Also, the entry-function, which is called by nftw(), belongs to the same class. That needs to be the case because the entry-function is supposed to change some class-members, dependent on the files that it finds.
When implementing such an approach, I get an error (see below). Is this an issue of syntax or is it not even possible to forward a pointer to a method to nftw()? In case it is not possible, do you know any alternative way to resursivly traverse a filesystem structure under linux?
class model
{
public:
boost::unordered_map<std::string, int> map;
int configure(const char *name)
{
// ...
ftw("DTModels/", this->ftw_entry, 15);
// ...
return = 0;
}
private:
int ftw_entry(const char *filepath, const struct stat *info, const int typeflag)
{
// Here I want to change the member 'map'
std::string filepath_s = filepath;
std::cout << "FTW_Entry: " << filepath_s << std::endl;
}
};
ERROR:
a pointer to a bound function may only be used to call the function
ftw("DTModels/", this->ftw_entry, 15);
c++ linux
add a comment |
I would like to use the ftw-function to recursivly traverse a filesystem structure. Additionally, the method shall be used inside of a class. Also, the entry-function, which is called by nftw(), belongs to the same class. That needs to be the case because the entry-function is supposed to change some class-members, dependent on the files that it finds.
When implementing such an approach, I get an error (see below). Is this an issue of syntax or is it not even possible to forward a pointer to a method to nftw()? In case it is not possible, do you know any alternative way to resursivly traverse a filesystem structure under linux?
class model
{
public:
boost::unordered_map<std::string, int> map;
int configure(const char *name)
{
// ...
ftw("DTModels/", this->ftw_entry, 15);
// ...
return = 0;
}
private:
int ftw_entry(const char *filepath, const struct stat *info, const int typeflag)
{
// Here I want to change the member 'map'
std::string filepath_s = filepath;
std::cout << "FTW_Entry: " << filepath_s << std::endl;
}
};
ERROR:
a pointer to a bound function may only be used to call the function
ftw("DTModels/", this->ftw_entry, 15);
c++ linux
1
a member function pointer is different from a function pointer as you need an instance to call a member function. There must be lots of duplicate questions around, maybe you find something if you search for "member function pointer"
– user463035818
Nov 23 '18 at 11:21
yes, but every time the function configure() is called, that happens because somewhere else in the code the function configure() is called through a specific object with type "model". Hence, when "being" in the function configure(), I should be able to make a function pointer to "my own" member function ftw_entry(), i.e. this->ftw_entry. What I suspect the error to be is that maybe the Linux-function ftw() does not accept a "member function pointer". What do you think?
– Daiz
Nov 23 '18 at 12:19
there is no "but". You need an instance to call a member function.ftw
expects aint (*fn)(const char *,const struct stat *ptr, int flag)
which is a different type than your member function. One solution would be to makeftw_entry
a free function and change the memberfilepath
already inconfigure
– user463035818
Nov 23 '18 at 12:24
add a comment |
I would like to use the ftw-function to recursivly traverse a filesystem structure. Additionally, the method shall be used inside of a class. Also, the entry-function, which is called by nftw(), belongs to the same class. That needs to be the case because the entry-function is supposed to change some class-members, dependent on the files that it finds.
When implementing such an approach, I get an error (see below). Is this an issue of syntax or is it not even possible to forward a pointer to a method to nftw()? In case it is not possible, do you know any alternative way to resursivly traverse a filesystem structure under linux?
class model
{
public:
boost::unordered_map<std::string, int> map;
int configure(const char *name)
{
// ...
ftw("DTModels/", this->ftw_entry, 15);
// ...
return = 0;
}
private:
int ftw_entry(const char *filepath, const struct stat *info, const int typeflag)
{
// Here I want to change the member 'map'
std::string filepath_s = filepath;
std::cout << "FTW_Entry: " << filepath_s << std::endl;
}
};
ERROR:
a pointer to a bound function may only be used to call the function
ftw("DTModels/", this->ftw_entry, 15);
c++ linux
I would like to use the ftw-function to recursivly traverse a filesystem structure. Additionally, the method shall be used inside of a class. Also, the entry-function, which is called by nftw(), belongs to the same class. That needs to be the case because the entry-function is supposed to change some class-members, dependent on the files that it finds.
When implementing such an approach, I get an error (see below). Is this an issue of syntax or is it not even possible to forward a pointer to a method to nftw()? In case it is not possible, do you know any alternative way to resursivly traverse a filesystem structure under linux?
class model
{
public:
boost::unordered_map<std::string, int> map;
int configure(const char *name)
{
// ...
ftw("DTModels/", this->ftw_entry, 15);
// ...
return = 0;
}
private:
int ftw_entry(const char *filepath, const struct stat *info, const int typeflag)
{
// Here I want to change the member 'map'
std::string filepath_s = filepath;
std::cout << "FTW_Entry: " << filepath_s << std::endl;
}
};
ERROR:
a pointer to a bound function may only be used to call the function
ftw("DTModels/", this->ftw_entry, 15);
c++ linux
c++ linux
edited Nov 23 '18 at 12:15
Daiz
asked Nov 23 '18 at 11:06
DaizDaiz
72110
72110
1
a member function pointer is different from a function pointer as you need an instance to call a member function. There must be lots of duplicate questions around, maybe you find something if you search for "member function pointer"
– user463035818
Nov 23 '18 at 11:21
yes, but every time the function configure() is called, that happens because somewhere else in the code the function configure() is called through a specific object with type "model". Hence, when "being" in the function configure(), I should be able to make a function pointer to "my own" member function ftw_entry(), i.e. this->ftw_entry. What I suspect the error to be is that maybe the Linux-function ftw() does not accept a "member function pointer". What do you think?
– Daiz
Nov 23 '18 at 12:19
there is no "but". You need an instance to call a member function.ftw
expects aint (*fn)(const char *,const struct stat *ptr, int flag)
which is a different type than your member function. One solution would be to makeftw_entry
a free function and change the memberfilepath
already inconfigure
– user463035818
Nov 23 '18 at 12:24
add a comment |
1
a member function pointer is different from a function pointer as you need an instance to call a member function. There must be lots of duplicate questions around, maybe you find something if you search for "member function pointer"
– user463035818
Nov 23 '18 at 11:21
yes, but every time the function configure() is called, that happens because somewhere else in the code the function configure() is called through a specific object with type "model". Hence, when "being" in the function configure(), I should be able to make a function pointer to "my own" member function ftw_entry(), i.e. this->ftw_entry. What I suspect the error to be is that maybe the Linux-function ftw() does not accept a "member function pointer". What do you think?
– Daiz
Nov 23 '18 at 12:19
there is no "but". You need an instance to call a member function.ftw
expects aint (*fn)(const char *,const struct stat *ptr, int flag)
which is a different type than your member function. One solution would be to makeftw_entry
a free function and change the memberfilepath
already inconfigure
– user463035818
Nov 23 '18 at 12:24
1
1
a member function pointer is different from a function pointer as you need an instance to call a member function. There must be lots of duplicate questions around, maybe you find something if you search for "member function pointer"
– user463035818
Nov 23 '18 at 11:21
a member function pointer is different from a function pointer as you need an instance to call a member function. There must be lots of duplicate questions around, maybe you find something if you search for "member function pointer"
– user463035818
Nov 23 '18 at 11:21
yes, but every time the function configure() is called, that happens because somewhere else in the code the function configure() is called through a specific object with type "model". Hence, when "being" in the function configure(), I should be able to make a function pointer to "my own" member function ftw_entry(), i.e. this->ftw_entry. What I suspect the error to be is that maybe the Linux-function ftw() does not accept a "member function pointer". What do you think?
– Daiz
Nov 23 '18 at 12:19
yes, but every time the function configure() is called, that happens because somewhere else in the code the function configure() is called through a specific object with type "model". Hence, when "being" in the function configure(), I should be able to make a function pointer to "my own" member function ftw_entry(), i.e. this->ftw_entry. What I suspect the error to be is that maybe the Linux-function ftw() does not accept a "member function pointer". What do you think?
– Daiz
Nov 23 '18 at 12:19
there is no "but". You need an instance to call a member function.
ftw
expects a int (*fn)(const char *,const struct stat *ptr, int flag)
which is a different type than your member function. One solution would be to make ftw_entry
a free function and change the member filepath
already in configure
– user463035818
Nov 23 '18 at 12:24
there is no "but". You need an instance to call a member function.
ftw
expects a int (*fn)(const char *,const struct stat *ptr, int flag)
which is a different type than your member function. One solution would be to make ftw_entry
a free function and change the member filepath
already in configure
– user463035818
Nov 23 '18 at 12:24
add a comment |
1 Answer
1
active
oldest
votes
I haven't used ftw
in many many years and since you ask for an alternative, take a look at std::filesystem
(C++17). Many pre-C++17 installations have it available via boost
or experimental
. If you use one of the pre-C++17 implementations, you many need to remove some of the stat
lines from the below to make it work.
#include <iostream>
//#define I_HAVE_BOOST
#if __cplusplus >= 201703L
#include <filesystem>
namespace fs = std::filesystem;
#elif I_HAVE_BOOST
#include <boost/filesystem.hpp>
namespace fs = boost::filesystem;
#else
#include <experimental/filesystem>
namespace fs = std::experimental::filesystem;
#endif
auto& out = std::cout;
void show_dent(const fs::directory_entry& dent) {
static size_t indent=0;
std::string ind(indent, ' ');
fs::file_status lstat = dent.symlink_status();
if( fs::is_symlink(lstat) ) {
fs::path pp = fs::read_symlink(dent);
out << ind << dent << " -> " << pp << "n";
++indent;
show_dent(fs::directory_entry(pp));
--indent;
} else {
if(fs::is_directory(dent)) {
fs::directory_iterator dit_end;
std::cout << "Directory " << dent << " includes the following files:n";
++indent;
for(auto dit = fs::directory_iterator(dent); dit != dit_end; ++dit) {
show_dent(*dit);
}
--indent;
} else {
fs::file_status stat = dent.status();
out << ind << dent << "n"
<< ind << " statn"
<< ind << " is_regular_file : " << fs::is_regular_file(stat) << "n"
<< ind << " is_directory : " << fs::is_directory(stat) << "n"
<< ind << " is_block_file : " << fs::is_block_file(stat) << "n"
<< ind << " is_character_file: " << fs::is_character_file(stat) << "n"
<< ind << " is_fifo : " << fs::is_fifo(stat) << "n"
<< ind << " is_socket : " << fs::is_socket(stat) << "n"
<< ind << " is_symlink : " << fs::is_symlink(stat) << "n"
<< ind << " exists : " << fs::exists(stat) << "n";
if( fs::is_regular_file(stat) ) {
out
<< ind << " file_size : " << fs::file_size(dent) << "n";
}
}
}
}
int main(int argc, char* argv) {
std::vector<std::string> args(argv+1, argv+argc);
out << std::boolalpha;
for(const auto& file_or_dir : args) {
show_dent(fs::directory_entry(file_or_dir));
}
return 0;
}
How will this work in the cases when the entries are moved, renamed, deleted while the traversal happens? What'll happen if the tree has too large nesting level? All this is handled bynftw
andftw
functions, which the OP was asking about, while I suppose the implementation you propose won't work normally in at least some of these situations. Making it work correctly is re-inventingftw
/nftw
. So it's not a valid alternative.
– Ruslan
Mar 21 at 9:17
I haven't looked at the actual implementation ofstd::filesystem
so I can't answer all those questions I'm afraid, but "If a file or a directory is deleted or added to the directory tree after the directory iterator has been created, it is unspecified whether the change would be observed through the iterator" and I expect (but can't confirm) thatstd::filesystem
uses the low-level OS APIs for directory traversal so that running out of file descriptors etc. won't be a problem. Just speculation on my part though.
– Ted Lyngmo
Mar 21 at 10:39
add a comment |
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53445524%2fcan-i-use-the-ftw-function-for-class-methods-in-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I haven't used ftw
in many many years and since you ask for an alternative, take a look at std::filesystem
(C++17). Many pre-C++17 installations have it available via boost
or experimental
. If you use one of the pre-C++17 implementations, you many need to remove some of the stat
lines from the below to make it work.
#include <iostream>
//#define I_HAVE_BOOST
#if __cplusplus >= 201703L
#include <filesystem>
namespace fs = std::filesystem;
#elif I_HAVE_BOOST
#include <boost/filesystem.hpp>
namespace fs = boost::filesystem;
#else
#include <experimental/filesystem>
namespace fs = std::experimental::filesystem;
#endif
auto& out = std::cout;
void show_dent(const fs::directory_entry& dent) {
static size_t indent=0;
std::string ind(indent, ' ');
fs::file_status lstat = dent.symlink_status();
if( fs::is_symlink(lstat) ) {
fs::path pp = fs::read_symlink(dent);
out << ind << dent << " -> " << pp << "n";
++indent;
show_dent(fs::directory_entry(pp));
--indent;
} else {
if(fs::is_directory(dent)) {
fs::directory_iterator dit_end;
std::cout << "Directory " << dent << " includes the following files:n";
++indent;
for(auto dit = fs::directory_iterator(dent); dit != dit_end; ++dit) {
show_dent(*dit);
}
--indent;
} else {
fs::file_status stat = dent.status();
out << ind << dent << "n"
<< ind << " statn"
<< ind << " is_regular_file : " << fs::is_regular_file(stat) << "n"
<< ind << " is_directory : " << fs::is_directory(stat) << "n"
<< ind << " is_block_file : " << fs::is_block_file(stat) << "n"
<< ind << " is_character_file: " << fs::is_character_file(stat) << "n"
<< ind << " is_fifo : " << fs::is_fifo(stat) << "n"
<< ind << " is_socket : " << fs::is_socket(stat) << "n"
<< ind << " is_symlink : " << fs::is_symlink(stat) << "n"
<< ind << " exists : " << fs::exists(stat) << "n";
if( fs::is_regular_file(stat) ) {
out
<< ind << " file_size : " << fs::file_size(dent) << "n";
}
}
}
}
int main(int argc, char* argv) {
std::vector<std::string> args(argv+1, argv+argc);
out << std::boolalpha;
for(const auto& file_or_dir : args) {
show_dent(fs::directory_entry(file_or_dir));
}
return 0;
}
How will this work in the cases when the entries are moved, renamed, deleted while the traversal happens? What'll happen if the tree has too large nesting level? All this is handled bynftw
andftw
functions, which the OP was asking about, while I suppose the implementation you propose won't work normally in at least some of these situations. Making it work correctly is re-inventingftw
/nftw
. So it's not a valid alternative.
– Ruslan
Mar 21 at 9:17
I haven't looked at the actual implementation ofstd::filesystem
so I can't answer all those questions I'm afraid, but "If a file or a directory is deleted or added to the directory tree after the directory iterator has been created, it is unspecified whether the change would be observed through the iterator" and I expect (but can't confirm) thatstd::filesystem
uses the low-level OS APIs for directory traversal so that running out of file descriptors etc. won't be a problem. Just speculation on my part though.
– Ted Lyngmo
Mar 21 at 10:39
add a comment |
I haven't used ftw
in many many years and since you ask for an alternative, take a look at std::filesystem
(C++17). Many pre-C++17 installations have it available via boost
or experimental
. If you use one of the pre-C++17 implementations, you many need to remove some of the stat
lines from the below to make it work.
#include <iostream>
//#define I_HAVE_BOOST
#if __cplusplus >= 201703L
#include <filesystem>
namespace fs = std::filesystem;
#elif I_HAVE_BOOST
#include <boost/filesystem.hpp>
namespace fs = boost::filesystem;
#else
#include <experimental/filesystem>
namespace fs = std::experimental::filesystem;
#endif
auto& out = std::cout;
void show_dent(const fs::directory_entry& dent) {
static size_t indent=0;
std::string ind(indent, ' ');
fs::file_status lstat = dent.symlink_status();
if( fs::is_symlink(lstat) ) {
fs::path pp = fs::read_symlink(dent);
out << ind << dent << " -> " << pp << "n";
++indent;
show_dent(fs::directory_entry(pp));
--indent;
} else {
if(fs::is_directory(dent)) {
fs::directory_iterator dit_end;
std::cout << "Directory " << dent << " includes the following files:n";
++indent;
for(auto dit = fs::directory_iterator(dent); dit != dit_end; ++dit) {
show_dent(*dit);
}
--indent;
} else {
fs::file_status stat = dent.status();
out << ind << dent << "n"
<< ind << " statn"
<< ind << " is_regular_file : " << fs::is_regular_file(stat) << "n"
<< ind << " is_directory : " << fs::is_directory(stat) << "n"
<< ind << " is_block_file : " << fs::is_block_file(stat) << "n"
<< ind << " is_character_file: " << fs::is_character_file(stat) << "n"
<< ind << " is_fifo : " << fs::is_fifo(stat) << "n"
<< ind << " is_socket : " << fs::is_socket(stat) << "n"
<< ind << " is_symlink : " << fs::is_symlink(stat) << "n"
<< ind << " exists : " << fs::exists(stat) << "n";
if( fs::is_regular_file(stat) ) {
out
<< ind << " file_size : " << fs::file_size(dent) << "n";
}
}
}
}
int main(int argc, char* argv) {
std::vector<std::string> args(argv+1, argv+argc);
out << std::boolalpha;
for(const auto& file_or_dir : args) {
show_dent(fs::directory_entry(file_or_dir));
}
return 0;
}
How will this work in the cases when the entries are moved, renamed, deleted while the traversal happens? What'll happen if the tree has too large nesting level? All this is handled bynftw
andftw
functions, which the OP was asking about, while I suppose the implementation you propose won't work normally in at least some of these situations. Making it work correctly is re-inventingftw
/nftw
. So it's not a valid alternative.
– Ruslan
Mar 21 at 9:17
I haven't looked at the actual implementation ofstd::filesystem
so I can't answer all those questions I'm afraid, but "If a file or a directory is deleted or added to the directory tree after the directory iterator has been created, it is unspecified whether the change would be observed through the iterator" and I expect (but can't confirm) thatstd::filesystem
uses the low-level OS APIs for directory traversal so that running out of file descriptors etc. won't be a problem. Just speculation on my part though.
– Ted Lyngmo
Mar 21 at 10:39
add a comment |
I haven't used ftw
in many many years and since you ask for an alternative, take a look at std::filesystem
(C++17). Many pre-C++17 installations have it available via boost
or experimental
. If you use one of the pre-C++17 implementations, you many need to remove some of the stat
lines from the below to make it work.
#include <iostream>
//#define I_HAVE_BOOST
#if __cplusplus >= 201703L
#include <filesystem>
namespace fs = std::filesystem;
#elif I_HAVE_BOOST
#include <boost/filesystem.hpp>
namespace fs = boost::filesystem;
#else
#include <experimental/filesystem>
namespace fs = std::experimental::filesystem;
#endif
auto& out = std::cout;
void show_dent(const fs::directory_entry& dent) {
static size_t indent=0;
std::string ind(indent, ' ');
fs::file_status lstat = dent.symlink_status();
if( fs::is_symlink(lstat) ) {
fs::path pp = fs::read_symlink(dent);
out << ind << dent << " -> " << pp << "n";
++indent;
show_dent(fs::directory_entry(pp));
--indent;
} else {
if(fs::is_directory(dent)) {
fs::directory_iterator dit_end;
std::cout << "Directory " << dent << " includes the following files:n";
++indent;
for(auto dit = fs::directory_iterator(dent); dit != dit_end; ++dit) {
show_dent(*dit);
}
--indent;
} else {
fs::file_status stat = dent.status();
out << ind << dent << "n"
<< ind << " statn"
<< ind << " is_regular_file : " << fs::is_regular_file(stat) << "n"
<< ind << " is_directory : " << fs::is_directory(stat) << "n"
<< ind << " is_block_file : " << fs::is_block_file(stat) << "n"
<< ind << " is_character_file: " << fs::is_character_file(stat) << "n"
<< ind << " is_fifo : " << fs::is_fifo(stat) << "n"
<< ind << " is_socket : " << fs::is_socket(stat) << "n"
<< ind << " is_symlink : " << fs::is_symlink(stat) << "n"
<< ind << " exists : " << fs::exists(stat) << "n";
if( fs::is_regular_file(stat) ) {
out
<< ind << " file_size : " << fs::file_size(dent) << "n";
}
}
}
}
int main(int argc, char* argv) {
std::vector<std::string> args(argv+1, argv+argc);
out << std::boolalpha;
for(const auto& file_or_dir : args) {
show_dent(fs::directory_entry(file_or_dir));
}
return 0;
}
I haven't used ftw
in many many years and since you ask for an alternative, take a look at std::filesystem
(C++17). Many pre-C++17 installations have it available via boost
or experimental
. If you use one of the pre-C++17 implementations, you many need to remove some of the stat
lines from the below to make it work.
#include <iostream>
//#define I_HAVE_BOOST
#if __cplusplus >= 201703L
#include <filesystem>
namespace fs = std::filesystem;
#elif I_HAVE_BOOST
#include <boost/filesystem.hpp>
namespace fs = boost::filesystem;
#else
#include <experimental/filesystem>
namespace fs = std::experimental::filesystem;
#endif
auto& out = std::cout;
void show_dent(const fs::directory_entry& dent) {
static size_t indent=0;
std::string ind(indent, ' ');
fs::file_status lstat = dent.symlink_status();
if( fs::is_symlink(lstat) ) {
fs::path pp = fs::read_symlink(dent);
out << ind << dent << " -> " << pp << "n";
++indent;
show_dent(fs::directory_entry(pp));
--indent;
} else {
if(fs::is_directory(dent)) {
fs::directory_iterator dit_end;
std::cout << "Directory " << dent << " includes the following files:n";
++indent;
for(auto dit = fs::directory_iterator(dent); dit != dit_end; ++dit) {
show_dent(*dit);
}
--indent;
} else {
fs::file_status stat = dent.status();
out << ind << dent << "n"
<< ind << " statn"
<< ind << " is_regular_file : " << fs::is_regular_file(stat) << "n"
<< ind << " is_directory : " << fs::is_directory(stat) << "n"
<< ind << " is_block_file : " << fs::is_block_file(stat) << "n"
<< ind << " is_character_file: " << fs::is_character_file(stat) << "n"
<< ind << " is_fifo : " << fs::is_fifo(stat) << "n"
<< ind << " is_socket : " << fs::is_socket(stat) << "n"
<< ind << " is_symlink : " << fs::is_symlink(stat) << "n"
<< ind << " exists : " << fs::exists(stat) << "n";
if( fs::is_regular_file(stat) ) {
out
<< ind << " file_size : " << fs::file_size(dent) << "n";
}
}
}
}
int main(int argc, char* argv) {
std::vector<std::string> args(argv+1, argv+argc);
out << std::boolalpha;
for(const auto& file_or_dir : args) {
show_dent(fs::directory_entry(file_or_dir));
}
return 0;
}
answered Nov 23 '18 at 12:48
Ted LyngmoTed Lyngmo
3,6082522
3,6082522
How will this work in the cases when the entries are moved, renamed, deleted while the traversal happens? What'll happen if the tree has too large nesting level? All this is handled bynftw
andftw
functions, which the OP was asking about, while I suppose the implementation you propose won't work normally in at least some of these situations. Making it work correctly is re-inventingftw
/nftw
. So it's not a valid alternative.
– Ruslan
Mar 21 at 9:17
I haven't looked at the actual implementation ofstd::filesystem
so I can't answer all those questions I'm afraid, but "If a file or a directory is deleted or added to the directory tree after the directory iterator has been created, it is unspecified whether the change would be observed through the iterator" and I expect (but can't confirm) thatstd::filesystem
uses the low-level OS APIs for directory traversal so that running out of file descriptors etc. won't be a problem. Just speculation on my part though.
– Ted Lyngmo
Mar 21 at 10:39
add a comment |
How will this work in the cases when the entries are moved, renamed, deleted while the traversal happens? What'll happen if the tree has too large nesting level? All this is handled bynftw
andftw
functions, which the OP was asking about, while I suppose the implementation you propose won't work normally in at least some of these situations. Making it work correctly is re-inventingftw
/nftw
. So it's not a valid alternative.
– Ruslan
Mar 21 at 9:17
I haven't looked at the actual implementation ofstd::filesystem
so I can't answer all those questions I'm afraid, but "If a file or a directory is deleted or added to the directory tree after the directory iterator has been created, it is unspecified whether the change would be observed through the iterator" and I expect (but can't confirm) thatstd::filesystem
uses the low-level OS APIs for directory traversal so that running out of file descriptors etc. won't be a problem. Just speculation on my part though.
– Ted Lyngmo
Mar 21 at 10:39
How will this work in the cases when the entries are moved, renamed, deleted while the traversal happens? What'll happen if the tree has too large nesting level? All this is handled by
nftw
and ftw
functions, which the OP was asking about, while I suppose the implementation you propose won't work normally in at least some of these situations. Making it work correctly is re-inventing ftw
/nftw
. So it's not a valid alternative.– Ruslan
Mar 21 at 9:17
How will this work in the cases when the entries are moved, renamed, deleted while the traversal happens? What'll happen if the tree has too large nesting level? All this is handled by
nftw
and ftw
functions, which the OP was asking about, while I suppose the implementation you propose won't work normally in at least some of these situations. Making it work correctly is re-inventing ftw
/nftw
. So it's not a valid alternative.– Ruslan
Mar 21 at 9:17
I haven't looked at the actual implementation of
std::filesystem
so I can't answer all those questions I'm afraid, but "If a file or a directory is deleted or added to the directory tree after the directory iterator has been created, it is unspecified whether the change would be observed through the iterator" and I expect (but can't confirm) that std::filesystem
uses the low-level OS APIs for directory traversal so that running out of file descriptors etc. won't be a problem. Just speculation on my part though.– Ted Lyngmo
Mar 21 at 10:39
I haven't looked at the actual implementation of
std::filesystem
so I can't answer all those questions I'm afraid, but "If a file or a directory is deleted or added to the directory tree after the directory iterator has been created, it is unspecified whether the change would be observed through the iterator" and I expect (but can't confirm) that std::filesystem
uses the low-level OS APIs for directory traversal so that running out of file descriptors etc. won't be a problem. Just speculation on my part though.– Ted Lyngmo
Mar 21 at 10:39
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53445524%2fcan-i-use-the-ftw-function-for-class-methods-in-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
a member function pointer is different from a function pointer as you need an instance to call a member function. There must be lots of duplicate questions around, maybe you find something if you search for "member function pointer"
– user463035818
Nov 23 '18 at 11:21
yes, but every time the function configure() is called, that happens because somewhere else in the code the function configure() is called through a specific object with type "model". Hence, when "being" in the function configure(), I should be able to make a function pointer to "my own" member function ftw_entry(), i.e. this->ftw_entry. What I suspect the error to be is that maybe the Linux-function ftw() does not accept a "member function pointer". What do you think?
– Daiz
Nov 23 '18 at 12:19
there is no "but". You need an instance to call a member function.
ftw
expects aint (*fn)(const char *,const struct stat *ptr, int flag)
which is a different type than your member function. One solution would be to makeftw_entry
a free function and change the memberfilepath
already inconfigure
– user463035818
Nov 23 '18 at 12:24