Denis Kotov
2018-10-09 18:11:13 UTC
Hello everyone,
I have prepared the proposal for extending an ADL search algorithm:
*Propousal for extension methods like.* Lets consider the following example:
namespace DeviceManager {
class Device {
public:
... // Other member functions
uint32_t getId() const {
return id_;
}
private:
... // Other member variables
uint32_t id_;
};
using DeviceList = std::vector<Device>;
Device mergeDeviceInfo(const Device & _first, const Device & _second)
{
... // Implementation
}
}
int main() {
DeviceManager::DeviceList deviceList;
... // Some work
auto foundDeviceIter0 = std::find_if(deviceList.begin(),
deviceList.end(), [=] {
... // Some first creteria
});
auto foundDeviceIter1 = std::find_if(deviceList.begin(),
deviceList.end(), [=] {
... // Some second creteria
});
if (deviceList.end() != foundDeviceIter0 && deviceList.end() !=
foundDeviceIter1) {
DeviceManager::Device mergedDevice =
DeviceManager::mergeDeviceInfo(*foundDeviceIter0, *foundDeviceIter1);
... // Some other work
}
}
As you can see in case one helper function it is easy to manipulate and use
it, but in case of a lot of functions it would be better to support some
kind of extension methods:
namespace DeviceManager {
class Device {
public:
... // Other member functions
uint32_t getId() const {
return id_;
}
private:
... // Other member variables
uint32_t id_;
};
using DeviceList = std::vector<Device>;
// 1. If it is called by reference type prefer this overload function
Device mergeDeviceInfo(const Device & _first, const Device & _second)
{
... // Implementation
}
// 2. If it is called by pointer type prefer this overload function
Device mergeDeviceInfo(const Device * _firstPtr, const Device *
_secondPtr) {
... // Implementation
}
}
int main() {
DeviceManager::DeviceList deviceList;
... // Some work
auto foundDeviceIter0 = std::find_if(deviceList.begin(),
deviceList.end(), [=] {
... // Some first creteria
});
auto foundDeviceIter1 = std::find_if(deviceList.begin(),
deviceList.end(), [=] {
... // Some second creteria
});
if (deviceList.end() != foundDeviceIter0 && deviceList.end() !=
foundDeviceIter1) {
// 1. Overload function for references if called
// ADL will search in dependent scopes also for calling functions
by object reference
DeviceManager::Device & firstFoundDevice = *foundDeviceIter0;
DeviceManager::Device & secondFoundDevice = *foundDeviceIter1;
DeviceManager::Device mergedDevice =
firstFoundDevice.mergeDeviceInfo(secondFoundDevice);
// 2. Overload function for pointers if called
// ADL will search in dependent scopes lso for calling functions by
object pointer
DeviceManager::Device mergedDevice =
foundDeviceIter0->mergeDeviceInfo(*foundDeviceIter1);
... // Some other work
}
}
*Propousal for extension of ADL on template parameter.* Lets consider the
following example:
namespace IPCBus {
class IClient {
public:
... // Other member functions
template <typename TClient>
static std::shared_ptr<IClient> buildClient() {
auto client = std::shared_ptr(new TClient);
... // IMPORTANT THINGS AFTER INITIALIZATION. CANNOT BE SKIPED
!!
return client;
}
protected:
Client() = default;
};
} // namespace IPCBus
class RealClient : public IClient {
public:
RealClient()
: IClient() {
}
};
using namespace std;
int main() {
auto realClient = make_shared<RealClient>();
// WE MISSED THE VERY IMPORTANT STEPS IN CREATION OF OBJECT !!
}
As you can see in case one helper function it is easy to manipulate and use
it, but in case of a lot of functions it would be better to support some
kind of extension methods:
namespace IPCBus {
class IClient {
public:
... // Other member functions
template <typename TClient>
static std::shared_ptr<IClient> buildClient() {
auto client = std::shared_ptr(new TClient);
... // IMPORTANT THINGS AFTER INITIALIZATION. CANNOT BE SKIPED
!!
return client;
}
protected:
Client() = default;
};
template <typename T, typename ... TArgs>
std::shared_ptr<T> make_shared(TArgs&&... _args) {
return IClient::buildClient(std::forward<TArgs>(_args)...);
}
} // namespace IPCBus
class RealClient : public IClient {
public:
RealClient()
: IClient() {
}
};
using namespace std;
int main() {
auto realClient = make_shared<RealClient>();
// We use our own version of make_shared that is delegate creation of
object to builder method
// Cool !!
}
As *Herb Sutter* said at *CppCon 2017: Herb Sutter âMeta: Thoughts on
generative C++â*:
"Abstraction are hiders by definition. If they did not do this they are
useless ..."
"It's not the problem. It is the point."
I see this suggestion to extend *ADL* search on caller object and on
template parameter as simplifier for Library Writters and also General
Programmers
I have prepared the proposal for extending an ADL search algorithm:
*Propousal for extension methods like.* Lets consider the following example:
namespace DeviceManager {
class Device {
public:
... // Other member functions
uint32_t getId() const {
return id_;
}
private:
... // Other member variables
uint32_t id_;
};
using DeviceList = std::vector<Device>;
Device mergeDeviceInfo(const Device & _first, const Device & _second)
{
... // Implementation
}
}
int main() {
DeviceManager::DeviceList deviceList;
... // Some work
auto foundDeviceIter0 = std::find_if(deviceList.begin(),
deviceList.end(), [=] {
... // Some first creteria
});
auto foundDeviceIter1 = std::find_if(deviceList.begin(),
deviceList.end(), [=] {
... // Some second creteria
});
if (deviceList.end() != foundDeviceIter0 && deviceList.end() !=
foundDeviceIter1) {
DeviceManager::Device mergedDevice =
DeviceManager::mergeDeviceInfo(*foundDeviceIter0, *foundDeviceIter1);
... // Some other work
}
}
As you can see in case one helper function it is easy to manipulate and use
it, but in case of a lot of functions it would be better to support some
kind of extension methods:
namespace DeviceManager {
class Device {
public:
... // Other member functions
uint32_t getId() const {
return id_;
}
private:
... // Other member variables
uint32_t id_;
};
using DeviceList = std::vector<Device>;
// 1. If it is called by reference type prefer this overload function
Device mergeDeviceInfo(const Device & _first, const Device & _second)
{
... // Implementation
}
// 2. If it is called by pointer type prefer this overload function
Device mergeDeviceInfo(const Device * _firstPtr, const Device *
_secondPtr) {
... // Implementation
}
}
int main() {
DeviceManager::DeviceList deviceList;
... // Some work
auto foundDeviceIter0 = std::find_if(deviceList.begin(),
deviceList.end(), [=] {
... // Some first creteria
});
auto foundDeviceIter1 = std::find_if(deviceList.begin(),
deviceList.end(), [=] {
... // Some second creteria
});
if (deviceList.end() != foundDeviceIter0 && deviceList.end() !=
foundDeviceIter1) {
// 1. Overload function for references if called
// ADL will search in dependent scopes also for calling functions
by object reference
DeviceManager::Device & firstFoundDevice = *foundDeviceIter0;
DeviceManager::Device & secondFoundDevice = *foundDeviceIter1;
DeviceManager::Device mergedDevice =
firstFoundDevice.mergeDeviceInfo(secondFoundDevice);
// 2. Overload function for pointers if called
// ADL will search in dependent scopes lso for calling functions by
object pointer
DeviceManager::Device mergedDevice =
foundDeviceIter0->mergeDeviceInfo(*foundDeviceIter1);
... // Some other work
}
}
*Propousal for extension of ADL on template parameter.* Lets consider the
following example:
namespace IPCBus {
class IClient {
public:
... // Other member functions
template <typename TClient>
static std::shared_ptr<IClient> buildClient() {
auto client = std::shared_ptr(new TClient);
... // IMPORTANT THINGS AFTER INITIALIZATION. CANNOT BE SKIPED
!!
return client;
}
protected:
Client() = default;
};
} // namespace IPCBus
class RealClient : public IClient {
public:
RealClient()
: IClient() {
}
};
using namespace std;
int main() {
auto realClient = make_shared<RealClient>();
// WE MISSED THE VERY IMPORTANT STEPS IN CREATION OF OBJECT !!
}
As you can see in case one helper function it is easy to manipulate and use
it, but in case of a lot of functions it would be better to support some
kind of extension methods:
namespace IPCBus {
class IClient {
public:
... // Other member functions
template <typename TClient>
static std::shared_ptr<IClient> buildClient() {
auto client = std::shared_ptr(new TClient);
... // IMPORTANT THINGS AFTER INITIALIZATION. CANNOT BE SKIPED
!!
return client;
}
protected:
Client() = default;
};
template <typename T, typename ... TArgs>
std::shared_ptr<T> make_shared(TArgs&&... _args) {
return IClient::buildClient(std::forward<TArgs>(_args)...);
}
} // namespace IPCBus
class RealClient : public IClient {
public:
RealClient()
: IClient() {
}
};
using namespace std;
int main() {
auto realClient = make_shared<RealClient>();
// We use our own version of make_shared that is delegate creation of
object to builder method
// Cool !!
}
As *Herb Sutter* said at *CppCon 2017: Herb Sutter âMeta: Thoughts on
generative C++â*:
"Abstraction are hiders by definition. If they did not do this they are
useless ..."
"It's not the problem. It is the point."
I see this suggestion to extend *ADL* search on caller object and on
template parameter as simplifier for Library Writters and also General
Programmers
--
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+***@isocpp.org.
To post to this group, send email to std-***@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/799e13cb-4354-4ffe-8568-276e1cbfe61c%40isocpp.org.
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+***@isocpp.org.
To post to this group, send email to std-***@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/799e13cb-4354-4ffe-8568-276e1cbfe61c%40isocpp.org.