« first day (1653 days earlier)      last day (1202 days later) » 

1:07 AM
@nwp As per cplusplus.com/reference/string/string/data, which says data is const.
const char* data() const noexcept; C++11
const char* data() const; C++98
 
1:29 AM
How to pass variable arguments from C++ to C interface?
 
1:44 AM
The C interface accepts a variable arguments, too.
 
 
1 hour later…
3:09 AM
@PeterT It seems that there is still a problem, here is the code snippet which can't complie:
class demo
{
struct data;

data dat;
};

struct demo::data{};
error: field ‘dat’ has incomplete type ‘demo::data’
And the correct code snippet should be something like this:
class demo
{
struct data{};

data dat;
};
 
3:48 AM
What is the most efficient std container to use if I need both efficient random access to element and need to frequently remove elements from "random" indices in the container? In my case, the platform the code will run on, and the amount of data I need to process, make even small improvements important.
I was looking at this: webcache.googleusercontent.com/…, however it seems that there is no container that is "efficient at both", according to this resource.
 
 
5 hours later…
8:40 AM
@KeithMadison You could, perhaps, try an unordered_map<size_t, element>. Expected O(1) time for either access by index or removing an element.
 
8:51 AM
@John yeah, you can't use it by value if it's just forward-declared
 
@PeterT The problem remains the same. The outer class(i.e class named demo) may grow very big as there are a lot of stuctures need to be defined.
 
well you have two options, either you define the full class before you use it by value. Or you don't store it by value, but use something like a unique_ptr to it instead
 
9:09 AM
@PeterT I see, thank you for the clarification.
Why this code snippet does not work? coliru.stacked-crooked.com/a/6a6ef3337030d36d
#include <stdio.h>
#include <stdarg.h>


int fooPrint(const char* fmt, ...)
{
va_list argp;
va_start(argp, fmt);
printf(fmt, argp);
va_end(argp);
return 0;
}

int main()
{
fooPrint("hello", "world");

return 0;
}
Solutions could be found in stackoverflow.com/questions/3530771/…, but I wonder why the aforementioned code snippet does not work.
 
@John because a "..." is not the same as a "va_list" parameter, just use "vprintf" ,etc if you already have the va_list
 
9:25 AM
The API that I need to call is defined in a public project(I should not modify the related code indeed). And the declaration of the API is something like this: int inner_error(const char*, ...).
Why this code snippet does not work either, what's lie behind "..."?. int fooPrint(const char* fmt, ...)
{
printf(fmt);
return 0;
}
 
 
1 hour later…
nwp
10:53 AM
@John Don't use cplusplus.com. It's old and wrong. Use cppreference (Also note the word "anymore", referring to the fact that it was changed).
 
@nwp c_str is const and data is not anymore? It seems that data is const. The declarations are:
const CharT* data() const;
(until C++11)
const CharT* data() const noexcept;
(since C++11)
(until C++20)
constexpr const CharT* data() const noexcept;
(since C++20)
(2)
CharT* data() noexcept;
(since C++17)
(until C++20)
constexpr CharT* data() noexcept;
 
nwp
constexpr CharT* data() noexcept; means that the CharT* is not const.
std::string s{"Hello"};
*s.data() = 'M';
That's valid code in C++17.
 
I see, thank you.
@nwp One more question, these two decalrations are both valid for C++20. How to know which one is really called?
constexpr const CharT* data() const noexcept;
constexpr CharT* data() noexcept;
 
11:13 AM
if the "std::string" instance is const then the version with "data() const" is called
 
nwp
The rules are that if the std::string you call .data() on is const (that's what the second const refers to) then it has to use the first, otherwise it uses the second.
In practice you don't really need to care since the designers tried to make it intuitive.
const std::string s{"Hello"};
*s.data() = 'M';
This fails to compile because s.data() returned a const char * here, but it also kinda makes sense, you can't modify a const std::string.
When making your own functions try to have all overloads do "the same thing" so that it doesn't matter to the user of the function which one is called.
 
@nwp @PeterT Thanks to both of you. My understanding of this question is at a different level with the help of you.
 
11:29 AM
hi
 
hello
 
12:26 PM
I've been given a task I don't really understand. It's about QT and network. I understand the basics; we use QNetworkReply class and it's method readAll. The task is:
if the mime type of the file (that we are getting through reply->header(QNetworkRequest::ContentTypeHeader) ) is textual, then we just write it to plainTextEdit. I've done that part. However, if the mime type isn't textual, we are supposed to download it through temporary file and that copy it into chosen file (I'm assuming he meant directory) from File Dialog. It says: "For generating the name of the temporary file use the Q
And I don't really know what to do now. Nothing makes sense.
Okay, good news: I've figured it out. Most part.
Still the part I don't get is how to use QDir class for copying.
 
nwp
That was quick.
 
hahahah yeah
 
nwp
Maybe use QDir::rename to make a copy.
It's probably not a good idea, but fulfills the requirements.
 
well, to move it
there's also
QFile::rename and QFile::copy
 
Is there any software that could effectively make notes when learning C++?
 
12:35 PM
Okay, thanks guys.
 
 
2 hours later…
2:25 PM
@JerryCoffin Yep :)
 
 
1 hour later…
3:25 PM
Does doc.qt.io/qt-5/qdir.html#rename deletes the old file?
I mean, I'm not sure what it does, that's all.
Because I rename a file with that function but I only get the copy in the new path. The old file doesn't get deleted,as I've expected
 
@kenkar did you close the old file?
 
no I haven't
 
because if not, then it just can't delete it
 
I see
it still doesn't work
 
nwp
Can you move the file manually? Just to check if permissions allow it.
 
3:30 PM
Yes I can
wait
it deletes it, I was wrong
my bad
so when I rename a QFile, what should I expect that fileName() function return afterwards ? The old name or the new name ?
 
like the non-static version of rename? The new name I would assume
 
QDir().rename(file.fileName(),basename);
ui->textPlainTextEdit->textCursor().insertText(file.fileName());
I'm getting old name
 
yeah, that's the static function, there you'll get the old name
 
I see
 
and "QDir::rename(a,b)" is how you would normally call it
 
3:36 PM
Yeah
I didn't want to create a Qdir object so I paid the price
 
it's a static function, you don't need an instance of the class
 
nwp
But you did create a QDir object. I'm not sure what you meant by that.
 
hmm
ok now it makes sense
i was super confused for a second
still confused
so QDir().rename is call of the static function
but rename is listed as public function and I can't find the static version in there
 
oh, nvm. QFile::rename was the one that has a static version
 
hm so why do I get the old name then?
 
3:50 PM
QDir().rename looks like it is accessing the member rename for a temporary default constructed instance of QDir.
 
what I meant was the non-static version of QFile::rename, if you use a version of rename that just takes a string of the filename, it's not going to update the QFile instance
file.rename(basename); is about the only version that will update the actual things inside the file
 
aha okay
I must use QDir so I would need to find alternative way to get the new name
 
the new name is the one that you told it to rename it to
 
yep, but I need also the current Path cuz the basename is just the name of the file ( not the name of the path )
 
QDir::currentPath() will give you the working directory
 
3:57 PM
thanks
 
4:56 PM
If I have this in my class, am I guaranteed that each created object has it's own unique id? Because right now I get two objects with the same id and it doesn't make sense to me:
int MyClass::id_counter_ = -11;
MyClass::MyClass() : id_(id_counter_--);
 
nwp
Every newly constructed object should get a new ID. However, if MyClass is copyable that won't necessarily stay true.
You could make it so the copy constructor generates a new ID.
 
It is copyable, atleast I do copy it, but I didn't implement the = operator, I guess it's using the default = operator, I thought this doesn't even work and I have to write a = operator myself, but I just assumed maybe c++ did some progress
 
nwp
C++ always had a default copy constructor that copies all the members.
Even C has that.
I think at least.
 
Hmmm, but sometimes it didn't work. I remember to have to implement myself one because of some obscure reason
 
@nwp (except when it can't, such as a member that's const or a reference).
 
nwp
5:01 PM
The issue is that copying all the members isn't necessarily correct.
Especially when you have pointers.
 
I have a larger class which holds foos and I assign the members in that class like this, but maybe there is a better way?
MyClass foo(a, b, c, d);
foo_ = foo;
 
nwp
Use initializer lists. The syntax takes some getting used to, but afterwards it's better.
Not to be confused with the other 2 things called initializer list in C++.
 
I know them, that's what I'm using for the id. The thing is getting a, b, c, d is a lot of setup and a pain. And I'm not sure that's easy to do in an initializer list
 
nwp
Can you show a more complete example?
 
Container::Container(x,y,z){
	a, b, c = do_stuff(x,y,z)
	MyClass foo(a, b, c, d);
	foo_ = foo;
}
I basically have this, and do_stuff defines variables and calls other functions to go from x,y,z to a, b,c so doing that in an initializer list seems cumbersome. If that is out, is the above the right way to define foo_?
 
nwp
5:08 PM
You should be able to put that in the initalizer list. If all else fails write a function that returns a MyClass.
 
omg my b, it was a copy paste error, just found it. Grrr man
It's getting late, time to go home
 
nwp
Good choice.
 

« first day (1653 days earlier)      last day (1202 days later) »