the best for everyone
I am not able to print the last line with printf
Владислав🇺🇦
Good evening, C++ enjoyers. (well, depending on your time zone) I stumbled upon quite strange behavior and cannot explain it nor google anything similar. It is unclear to me why foo.func(const_lambda) in the following example would call non-const member function since it is clearly not suitable for given argument. In the main code where I try to use similar code it is not calling non-const member function but fails to even compile (the error is similar to the one generated if the argument is explicit std::function<void(const std::string&)>(const_lambda): "2 overloads have similar conversions"). I think it may be because of the way std::function is implemented but the source code is too complicated for me to understand. Can you please analyse the following snippet and help me detect the issue? https://cppinsights.io/s/63e9730e Updated link https://cppinsights.io/s/971f0067 Full error message for the commented code for msvc compiler: <source>(27): error C2666: 'Foo::func': 2 overloads have similar conversions <source>(7): note: could be 'void Foo::func(const std::function<void (std::string &)> &)' <source>(6): note: or 'void Foo::func(const std::function<void (const std::string &)> &) const' <source>(27): note: while trying to match the argument list '(const std::function<void (const std::string &)>)' <source>(27): note: note: qualification adjustment (const/volatile) may be causing the ambiguity
I was lucky enough to figure it out on my own. The answer was 'googleble', but I used other words at first😅 https://stackoverflow.com/questions/6193734/implicit-conversions-with-stdfunction
Mickey
How I can in gdb go back 1 step ?
Ludovic 'Archivist'
How I can in gdb go back 1 step ?
Can you unbreak an egg or unsplit an atom of uranium?
DaviChan
Can you unbreak an egg or unsplit an atom of uranium?
you can actually step back in gdb. I can't recall how it works exactly, but there where at least 2 ways to do that. One recorded the exection like qira does, so the debugging basically becomes "stateless". And the other one tries to reconstruct the state. Basically tracing back. This however, ofc. doesn't always work and also the recording of the execution is only practical or possible for small binaries
DaviChan
think the command was reverse-step or rs
DaviChan
or was it reverse-next
DaviChan
also not 100% if this is gdb native as I do have peda installed also
Anonymous
Good evening, C++ enjoyers. (well, depending on your time zone) I stumbled upon quite strange behavior and cannot explain it nor google anything similar. It is unclear to me why foo.func(const_lambda) in the following example would call non-const member function since it is clearly not suitable for given argument. In the main code where I try to use similar code it is not calling non-const member function but fails to even compile (the error is similar to the one generated if the argument is explicit std::function<void(const std::string&)>(const_lambda): "2 overloads have similar conversions"). I think it may be because of the way std::function is implemented but the source code is too complicated for me to understand. Can you please analyse the following snippet and help me detect the issue? https://cppinsights.io/s/63e9730e Updated link https://cppinsights.io/s/971f0067 Full error message for the commented code for msvc compiler: <source>(27): error C2666: 'Foo::func': 2 overloads have similar conversions <source>(7): note: could be 'void Foo::func(const std::function<void (std::string &)> &)' <source>(6): note: or 'void Foo::func(const std::function<void (const std::string &)> &) const' <source>(27): note: while trying to match the argument list '(const std::function<void (const std::string &)>)' <source>(27): note: note: qualification adjustment (const/volatile) may be causing the ambiguity
You have to understand overload resolution rules to be able to explain what happens. If two or more functions are viable candidates for a function call and 1. if one of the function has atleast one argument which is a better match for that argument than every other function in the viable candidates list 2. If that function offers a match for every other argument that is not worse than any other viable function then that function is chosen by overload resolution. If option 2 is not possible and if there are atleast 2 functions in the viable call list then the call will be ambiguous. Now in your case, your methods have an implicit parameter that is Foo* const in the non const version and Foo const* const in the const method. Now let us analyze your function calls line by line foo.func(lambda); // non-const Here foo is a non const object. So both method calls are viable. The non const method is a better match for the implicit this parameter. As for as the second parameter is concerned, both of them require a user defined conversion from a lambda to a std::function. The compiler doesn't rank user defined conversions and they are both considered to be equally good as far as this parameter is considered. So the match for the first parameter alone is taken into consideration and so the non const method is a better match and that is what ends up getting called. foo.func(const_lambda); // non-const why? Here again both method calls are viable. The same argument as that for the previous line applies Now you may ask how I can store a const_lambda in std::function<void(std::string&)>. Shouldn't the compiler ignore the non const method in this case? Well this is a common mistake that many people make. People forget that std::function is a template class wrapper and its constructor is also a template function. So std::function constructor can be called with any argument as long as there is a conversion from the template type parameters of std::function to the parameters of the wrapped up function or lambda. In this case, there is a conversion from std::string& to const std::string&. So wrapping a [](const std::string&){} using std::function<void(std::string&)> is acceptable. On the other hand wrapping a [](std::string&){} in std::function<void(const std::string&)> will not be accepted by the compiler because there is no conversion from const std::string& to std::string& const_foo.func(lambda); // expected error Here only one method call (the const version) is viable. However std::function<void(const std::string&)> cannot wrap a [](std::string&){} as mentioned in my comments for the line above. So this is flagged as an error. const_foo.func(const_lambda); // const Here again, only the const method is viable and it is also a match. So that method ends up getting called. const auto function = std::function<void(const std::string&)>(const_lambda); foo.func(function); Unlike the previous calls, where the second parameter involved a user defined conversion from the Closure type to a std::function, here the second parameter is a std::function. As far as the first parameter(this) is concerned, the non const method is a better match. As far as the 2nd parameter is concerned, the const method is a better match. So based on the overload resolution rules I mentioned at the start of the post, neither function is a better match than the other and hence the call is ambiguous.
H
I am getting SegV in destructor. I am not able to debug it. Anyone can help what could go wrong. I have made sure that there are nonpointer which has men leak.
H
I mean I am asking if anyone faced these situations, what could be the possibilities and how to debug?
Pavel
I am getting SegV in destructor. I am not able to debug it. Anyone can help what could go wrong. I have made sure that there are nonpointer which has men leak.
You're not able to debug because you can't break in debugger in that place? Or you can but it doesn't make sense for you what happens? Did you try to disable optimizations? Does your destructor do some logic? Dou you store some custom smart pointers or containers in your class?
Pavel
If you store just raw pointer to it, then you need to delete it manually. You can store it as std::unique_ptr in your map, then it will be deleted automatically as soon as you erase it from the map
Pavel
If you store std::unique_ptr you don't need to delete it manually, but you can call reset() on it, then the object will be deleted and unique_ptr will remember that it doesn't need to delete it again. If you store raw pointers and want to delete some object but keep the map record, then you can do the same by setting the value in the map to nullptr.
Pavel
If you try to delete some pointer twice, it will be an undefined behavior
Pavel
Class has pointers to an object.
Does it do anything with the pointer in the destructor, accessing it?
Pavel
Sometimes there can be issues with accessing dangling pointers in destructors because of destruction order
H
Thanks Pavel. Iam thinking so that would be the point. How to check when ptr has become null?
H
I am using default destructor only, there is nothing in destructor
Pavel
Thanks Pavel. Iam thinking so that would be the point. How to check when ptr has become null?
If you store a pointer to another class, and that class is destroyed, your pointer will become a dangling pointer. You can't check for that unfortunately. So there are a few things that you can do: - store std::shared_ptr (but it can create other problems with dependencies) - ensure you destroy classes in dependency order (usually it's enough to store them as members and ensure they go in correct order) struct A { MyObj obj; // constructed first, destriyed last MyObj2 obj2{&obj}; // constructed last, destroyed first }; class A { public: A(); private: MyObj obj; // constructed first, destriyed last MyObj2 obj2; // constructed last, destroyed first }; A:A() : obj2(&obj) {} Here if obj2 stores reference to obj, then it's completely safe, because obj2 will be always destroyed first. The order is the same in both cases, because of order of fields in the struct/class. This has a potential to break: class A { public: A(); private: MyObj2 obj2; MyObj obj; }; A:A() : obj2(&obj) {}
KEYUR
Nice
Pavel
I am using default destructor only, there is nothing in destructor
Hm, this is interesting, can you share your code?
Владислав🇺🇦
You have to understand overload resolution rules to be able to explain what happens. If two or more functions are viable candidates for a function call and 1. if one of the function has atleast one argument which is a better match for that argument than every other function in the viable candidates list 2. If that function offers a match for every other argument that is not worse than any other viable function then that function is chosen by overload resolution. If option 2 is not possible and if there are atleast 2 functions in the viable call list then the call will be ambiguous. Now in your case, your methods have an implicit parameter that is Foo* const in the non const version and Foo const* const in the const method. Now let us analyze your function calls line by line foo.func(lambda); // non-const Here foo is a non const object. So both method calls are viable. The non const method is a better match for the implicit this parameter. As for as the second parameter is concerned, both of them require a user defined conversion from a lambda to a std::function. The compiler doesn't rank user defined conversions and they are both considered to be equally good as far as this parameter is considered. So the match for the first parameter alone is taken into consideration and so the non const method is a better match and that is what ends up getting called. foo.func(const_lambda); // non-const why? Here again both method calls are viable. The same argument as that for the previous line applies Now you may ask how I can store a const_lambda in std::function<void(std::string&)>. Shouldn't the compiler ignore the non const method in this case? Well this is a common mistake that many people make. People forget that std::function is a template class wrapper and its constructor is also a template function. So std::function constructor can be called with any argument as long as there is a conversion from the template type parameters of std::function to the parameters of the wrapped up function or lambda. In this case, there is a conversion from std::string& to const std::string&. So wrapping a [](const std::string&){} using std::function<void(std::string&)> is acceptable. On the other hand wrapping a [](std::string&){} in std::function<void(const std::string&)> will not be accepted by the compiler because there is no conversion from const std::string& to std::string& const_foo.func(lambda); // expected error Here only one method call (the const version) is viable. However std::function<void(const std::string&)> cannot wrap a [](std::string&){} as mentioned in my comments for the line above. So this is flagged as an error. const_foo.func(const_lambda); // const Here again, only the const method is viable and it is also a match. So that method ends up getting called. const auto function = std::function<void(const std::string&)>(const_lambda); foo.func(function); Unlike the previous calls, where the second parameter involved a user defined conversion from the Closure type to a std::function, here the second parameter is a std::function. As far as the first parameter(this) is concerned, the non const method is a better match. As far as the 2nd parameter is concerned, the const method is a better match. So based on the overload resolution rules I mentioned at the start of the post, neither function is a better match than the other and hence the call is ambiguous.
Thank you for such a detailed answer!
Nobody
Hello guys, I have one question. Could you please tell me? Char *ptr = "India"; Where it will stored In the memory segment? Stack segment or read-only segment.
Anonymous
Thank you for such a detailed answer!
You would be better off asking such detailed questions on stackoverflow or reddit. It is not convenient to answer such questions in detail on a mobile app (yes I am aware of telegram web).
Anonymous
Hello guys, I have one question. Could you please tell me? Char *ptr = "India"; Where it will stored In the memory segment? Stack segment or read-only segment.
It is not a question that C/C++ standard addresses. This is OS specific. The string "India" is stored in a read-only section of the executable (which segment it is stored in depends on the type of executable like PE, COFF, ELF etc). The variable char* ptr is stored in the stack if declared within a function or in the data segment if it is defined outside functions.
Nobody
Sure Madhu
Nobody
Thank you for the answer
Void
int n,i=1,count=1,sum=0; scanf("%d",&n); while(n/=10) {count++; } printf("count=%d\n",count);
Planet
Can anyone help me in conding problem?
▪️fateme👷🏻‍♀️
int conditionalsum(int a, int b, int (*ptr)()) {       a=(*ptr)(a);       b=(*ptr)(b);       return a+b; } int square(int a) { return a*a; } int main() { int (*fp)(int); fp=square; Int sum=conditionalsum(2,3,fp); Our called back function is (int square(int a)) My question) why when we use int(*ptr)() as parameter in our fucntion definition?and still it's correct Why don't we use int(*ptr)(int)? I was thinking because of the presence of (a) in square function it will be correct to use Int(*ptr)(int), so how the use of int(*ptr)() is still correct???
ilya
how to write restapi and server
Scriptern
#res
Fr
Hi , I have taked all the lessons about the c programming language, what should I do next? And where?
Fr
What is the best platform to learn data structure in c?
BLESSED
What is the best platform to learn data structure in c?
i can recommend leetcode - but it only if u know basics.
BLESSED
Hi , I have taked all the lessons about the c programming language, what should I do next? And where?
you should start to write real projects. Firstly - decide what you want to do - just google "junior C programmer" and read all the requirements for this job. Also you can check any "C developer roadmap". Knowing algorithms is good but without practice you will forget them after a while.
BLESSED
Is it paid site?
it is free but also has premium subscription
Anonymous
What is the best platform to learn data structure in c?
Udemy offers this course .Algorithms and data structures are a requirement to be a developer learning just basics and syntax is not the most important in software development, its the algorithms that u use in solving problems, u cant be a competitive programmer without knowing algorithms & data structures.
Void
#include <stdio.h> int main() { int n,i=1,count=1,c,sum=0,a; scanf("%d",&n); while(n/=10) {count++; } c=count; while(i<=c) { a=n%10; sum=sum+a; n=n/10; i++; } printf("count=%d\n",count); printf("sum=%d",sum); return 0; }
Void
Guys can you see what's wrong with my code?
▪️fateme👷🏻‍♀️
#include<stdio.h> int max(); int main() { int arr[7]={1,22,5,43,6,42,6},n=7; int result=max(arr,n); printf("%d",result); } int max(int* arr,int n) { int i; for(i=0;i<n;i++) { arr[i]=arr[i]+1; } } I want whole array as output with everyone get +1 but it didn't work as my expectation Can anybody solve this problem without using return
Nyakio
What is the best platform to learn data structure in c?
https://www.cs.yale.edu/homes/aspnes/classes/223/notes.html#c
Void
Why without using return?
Can you check my code😭
Void
what are you trying to do ?
Find sum of digits of a number
adnanhossainme
adnanhossainme
Do it within 1 loop..no need i++
Void
I think i solved it...thanks!😊
Void
#include <stdio.h> int main() { int n,count=0,c,sum=0,a; scanf("%d",&n); while(n>0) { a=n%10; sum=sum+a; n=n/10; count++; } printf("count=%d\n",count); printf("sum=%d",sum); return 0; }
Void
Maybe it can be optimized
mito
adnanhossainme
I literally did the same.
Me too..except n!==0
mito
#include <stdio.h> int main() { int n; int count = 0; int sum = 0; printf("Enter the number : "); scanf("%d", &n); while (n > 0) { sum = sum + (n % 10); n = n / 10; count++; } printf("Count = %d\n", count); printf("Sum = %d\n", sum); return 0; } Look
adnanhossainme
I have two loop problems..any one wanna solve? It Will be must hard to solve
LE
hey i'm making a compiler in cpp and which data structure should i use for the symbol table?
adnanhossainme
²
Wanna try?
send to chat