Forum Replies Created

Viewing 14 posts - 1 through 14 (of 14 total)
  • Author
    Posts
  • in reply to: std::weak_ptr dereferencing #637668
    Gaurav Singh
    Participant
        Up
        0
        Down
        ::

        Hi @Michele,

        weak_ptr has no constructor or assignment operator which will take r-value shared_ptr. So in my understanding here std:move does not move rather it just copies the content.

        https://en.cppreference.com/w/cpp/memory/weak_ptr/weak_ptr

        https://en.cppreference.com/w/cpp/memory/weak_ptr/operator%3D

        Below

        int main()
        {
        std::weak_ptr<int> wp;
        {
        std::shared_ptr<int> sp = std::make_shared<int>(1);
        wp = std::move(sp);
        }
        }

        Internally it looks like this.

        std::weak_ptr<int> wp = std::weak_ptr<int>();
        {
            std::shared_ptr<int> sp = std::make_shared<int>(1);
            wp.operator=(static_cast<const std::shared_ptr<int> &&>(std::move(sp)));
        };

         

        You can also experiment here https://cppinsights.io/

        Gaurav Singh
        Participant
            Up
            0
            Down
            ::

            Hi Rainer,

            So to synchronize the static member on class level, then instead of having the mutex as a member variable, it should be a global mutex?

             

            in reply to: Managing resource with a wrapper #633099
            Gaurav Singh
            Participant
                Up
                1
                Down
                ::

                Hi Diana,

                so that the only way to interact with the resource is through the wrapper

                If all the functionality is provided by ResourceHandle then use will use the wrapper once to get the handle and then will continue to use the handle.

                Since you also mention unique_ptr, and you want Resource should be used similar to unique_ptr then you will have to overload -> and * operators.

                Also, one more interesting point to look for when providing such wrapper is https://en.cppreference.com/w/cpp/experimental/propagate_const.

                in reply to: pure virtual function, virtual destructor #633057
                Gaurav Singh
                Participant
                    Up
                    0
                    Down
                    ::

                    Abstract Class: If a class has a pure virtual function then that cannot be instantiated, this is what abstract means.

                    Whether one should declare class destructor as virtual or not might depend on:
                    1. If a class has any virtual function, then destructor should be declared virtual
                    2. If you do not intend to use the class using Base class pointer and always through value semantics then you can skip making destructor as virtual. In this case you do not intend to use runtime polymorphism.

                    in reply to: Weird behavior in CPP insights #632750
                    Gaurav Singh
                    Participant
                        Up
                        1
                        Down
                        ::

                        It is instantiating partial template in cppinsights and compiler explorer.

                        /* First instantiated from: insights.cpp:26 */
                        #ifdef INSIGHTS_USE_TEMPLATE
                        template<>
                        class tempC<int, 3, 3>
                        {
                        
                        public:
                        inline tempC()
                        {
                        std::operator<<(std::cout, "Partial template instantiated...\n");
                        }
                        
                        };
                        
                        #endif
                        
                        Gaurav Singh
                        Participant
                            Up
                            1
                            Down
                            ::

                            Divide by zero is not an exception but a signal SIGFPE (Floating point exception). The term has exception in its name, but its not a c++ exception.

                            in reply to: pure virtual function, virtual destructor #629863
                            Gaurav Singh
                            Participant
                                Up
                                0
                                Down
                                ::

                                Actually you can have a pure virtual destructor but then you have to provide the implementation of the destructor outside of the class.

                                Also if you declare destructor as pure virtual then, the class becomes abstract class.

                                https://godbolt.org/z/q74EoTozs

                                #include <iostream>
                                #include <memory>
                                
                                struct Base {
                                    Base() { std::cout << __func__ << std::endl; }
                                    virtual ~Base() = 0;
                                };
                                
                                Base::~Base() { std::cout << __func__ << std::endl; }
                                
                                struct Derived : Base {
                                    Derived()  { std::cout << __func__ << std::endl; }
                                    ~Derived() { std::cout << __func__ << std::endl; }
                                };
                                
                                int main() {
                                    // Base b; // Base is an abstract class
                                    {
                                        Derived d;   
                                    }
                                    {
                                        std::cout << "Polymorphic construction" << std::endl;
                                        std::shared_ptr<Base> b = std::make_shared<Derived>();
                                    }
                                }
                                
                                in reply to: How to manage user Secrets? #629862
                                Gaurav Singh
                                Participant
                                    Up
                                    -1
                                    Down
                                    ::

                                    Hi Fabio,

                                    There are few options
                                    1. You can manage the secrets through environment variables, which your code can use

                                    2. Put it in configuration file

                                    3. Encrypt/Decrypt the config file but then you have to maintain the keys for encryption

                                    4. Use external library eg: https://cloud.google.com/cpp/docs/reference/secretmanager/latest

                                    in reply to: Industries and projects #629689
                                    Gaurav Singh
                                    Participant
                                        Up
                                        0
                                        Down
                                        ::

                                        I work in automotive domain, mostly catering to infotainment system. I also have experience in medical domain developing software for devices used in critical care systems. Most of my work involves creating software using C++ and python. Currently I handle how to update the whole car and manage everything around the update system and stability of car.

                                        in reply to: Question about covariant return types #629521
                                        Gaurav Singh
                                        Participant
                                            Up
                                            0
                                            Down
                                            ::

                                            C++ functions should return by value, this is fine. All pointers are of same size this is also fine.

                                            But if you see, std::unique_ptr is just a wrapper on top of memory, so they should be as good as pointers. And by this I mean size of unique_ptr for Base or Derived should be same.

                                             

                                            #include <iostream>
                                            #include <memory>
                                            struct Base {
                                                int x;
                                            };
                                            
                                            struct Derived : Base {
                                                int y;
                                                int z;
                                                double d;
                                            };
                                            
                                            int main() {
                                                std::unique_ptr<Base> bp;
                                                std::unique_ptr<Derived> dp;
                                                std::cout << sizeof(bp) << " " << sizeof(dp) << std::endl;
                                                bp = std::make_unique<Base>();
                                                dp = std::make_unique<Derived>();
                                                std::cout << sizeof(bp) << " " << sizeof(dp) << std::endl;
                                            }
                                            

                                            Here is the output.

                                            8 8
                                            8 8

                                             

                                            in reply to: Line 31 of nullObject.Cpp contains an error? #629492
                                            Gaurav Singh
                                            Participant
                                                Up
                                                0
                                                Down
                                                ::

                                                http://www.modernescpp.com/index.php/the-null-object-pattern

                                                The locks NoLock (line 1), ExclusiveLock (line 2), and SharedLock (line 3) have no abstract base class. The consequence is that StrategizedLocking can be instantiated with an object that does not support the right interface. This instantiation would end in a compile-time error. This loophole is closed with C++20.

                                                The above post mentions a line that with template based implementation (prior to c++ 20) has a issue that any class that does not support the interface can be plugged.
                                                In my understanding this should not be possible as, this will be checked during compile time.

                                                Is my understanding correct or am I missing any point?

                                                Below is the code and error.

                                                template <typename Lock>
                                                class StrategizedLocking {
                                                    Lock& lock;
                                                public:
                                                    StrategizedLocking(Lock& l): lock(l){
                                                        lock.lock();
                                                    }
                                                    ~StrategizedLocking(){
                                                        lock.unlock();
                                                    }
                                                };
                                                
                                                struct InvalidClass {
                                                };
                                                
                                                
                                                int main() {
                                                    InvalidClass invalid;
                                                    StrategizedLocking<DummyClass> strat{invalid};
                                                }
                                                

                                                 

                                                
                                                main.cpp: In instantiation of 'StrategizedLocking::StrategizedLocking(Lock&) [with Lock = InvalidClass]':
                                                main.cpp:22:51: required from here
                                                main.cpp:9:14: error: 'struct InvalidClass' has no member named 'lock'
                                                9 | lock.lock();
                                                
                                                in reply to: Static_assert fails within consteval function #629185
                                                Gaurav Singh
                                                Participant
                                                    Up
                                                    0
                                                    Down
                                                    ::

                                                    Addding to above question. static_assert is always evaluated even if it is non compiling block.

                                                    #include <iostream>
                                                    
                                                    constexpr int foo(int v1, int v2) {
                                                      constexpr int x = 9;
                                                      constexpr int y = 9;
                                                      if constexpr (!std::is_same<decltype(v1), int>::value) {
                                                        static_assert(x != y);
                                                        return 1;
                                                      }
                                                      return 2;
                                                    }
                                                    
                                                    int main() {
                                                      constexpr int f = foo(1, 1);
                                                      static_assert(f == 2);
                                                    }
                                                    
                                                    in reply to: Factory method as static member of base class #628806
                                                    Gaurav Singh
                                                    Participant
                                                        Up
                                                        0
                                                        Down
                                                        ::

                                                        I guess it was misunderstood, I meant receiving (function parameter). Why in this case auto createWindow(std::unique_ptr<Window>& window) window is received by reference.

                                                        https://github.com/RainerGrimm/DesignPatternsAndArchitecturalPatterns/blob/6f249ec5da30c48a36255d8eea43f78d2f610544/factoryMethodUniquePtr.cpp#L29

                                                        in reply to: Factory method as static member of base class #628780
                                                        Gaurav Singh
                                                        Participant
                                                            Up
                                                            0
                                                            Down
                                                            ::

                                                            In a factory pattern using unique_ptr

                                                            auto createWindow(std::unique_ptr<Window>& window) {
                                                            return window->create()
                                                            }

                                                            Is there a specific reason why this function is receiving unique_ptr by reference?

                                                          Viewing 14 posts - 1 through 14 (of 14 total)