Forum Replies Created
-
AuthorPosts
-
::
Hi @Michele,
weak_ptr
has no constructor or assignment operator which will take r-valueshared_ptr
. So in my understanding herestd: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/
::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 tounique_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.
::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.30. September 2023 at 16:09 in reply to: std::bind_front and forced exception from the inderlying callable #629866::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>(); } }
::Hi Fabio,
There are few options
1. You can manage the secrets through environment variables, which your code can use2. 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
::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.
::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::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();
::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); }
::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. -
AuthorPosts