gpt4 book ai didi

模板函数参数的 C++17 invoke_result 和 static_assert

转载 作者:太空狗 更新时间:2023-10-29 21:11:13 27 4
gpt4 key购买 nike

这纯粹是为了在做泛型编程时获得更多知识。如何确保作为模板参数传递给另一个函数的函数的返回类型,该函数可以采用不同数量的参数(0 到 N)。

编辑:我正在尝试使用 std::invoke_resultstatic_assert() 来确保注册工厂方法的正确返回类型。我使用了一个比第一个发布的例子更好的例子来提供更多的清晰度。

#include <string>
#include <memory>
#include <typeindex>
#include <typeinfo>
#include <unordered_map>

using namespace std;

class Factory final
{
public:
template<typename My_Type, typename Func>
static bool Register(Func func)
{
// issue is these two lines of code, when trying to register Derived1 and Derived2 create functions
typename invoke_result<Func>::type result;
static_assert(is_same<decltype(result), unique_ptr<My_Type>>::value, "Not a unique pointer to type 'My_Type'");

bool isRegistered = false;

if (GetCreateFunctions().end() == GetCreateFunctions().find(typeid(My_Type)))
{
GetCreateFunctions()[typeid(My_Type)] = reinterpret_cast<void*>(func);
isRegistered = true;
}

return isRegistered;
}

template<typename My_Type, typename... Args>
static unique_ptr<My_Type> Create(Args&&... args)
{
unique_ptr<My_Type> type = nullptr;
auto iter = GetCreateFunctions().find(typeid(My_Type));

if (GetCreateFunctions().end() != iter)
{
typedef unique_ptr<My_Type>(*create_func)(Args&&...);
auto create = reinterpret_cast<create_func>(iter->second);
type = create(forward<Args>(args)...);
}

return type;
}

private:
static unordered_map<type_index, void*>& GetCreateFunctions()
{
static unordered_map<type_index, void*> map;
return map;
}
};

class Base
{
public:
Base(unique_ptr<string>&& moveString)
:
_moveString(move(moveString))
{
}

virtual ~Base() = default;
virtual void DoSomething() const = 0;

protected:
unique_ptr<string> _moveString;
};

class Derived1 final : public Base
{
public:
Derived1(unique_ptr<string>&& moveString)
:
Base(move(moveString))
{
}

~Derived1() = default;

void DoSomething() const override
{
if (_moveString)
{
// do something...
}
}

private:
static const bool _isRegistered;

static unique_ptr<Derived1> Create(unique_ptr<string>&& moveString)
{
return make_unique<Derived1>(move(moveString));
}
};

const bool Derived1::_isRegistered = Factory::template Register<Derived1>(&Derived1::Create);

class Derived2 final : public Base
{
public:
Derived2()
:
Base(make_unique<string>("Default"))
{
}

~Derived2() = default;

void DoSomething() const override
{
if (_moveString)
{
// do something...
}
}

private:
static const bool _isRegistered;

static unique_ptr<Derived2> Create()
{
return make_unique<Derived2>();
}
};

const bool Derived2::_isRegistered = Factory::template Register<Derived2>(&Derived2::Create);


int main(int argc, char** argv)
{
string moveString = "moveString";
unique_ptr<Base> myBase_Derived1 = Factory::template Create<Derived1>(make_unique<string>(move(moveString)));
unique_ptr<Base> myBase_Derived2 = Factory::template Create<Derived2>();

if (myBase_Derived1)
printf("Success\n");

if (myBase_Derived2)
printf("Success\n");

return 0;
}

最佳答案

你在

上有一个额外的 ()
static_assert(std::is_same<decltype(result), std::unique_ptr<T>/* here */ >::value, "Not a unique pointer to type 'T'");

typename std::invoke_result<Func/* here */>::type result;

AA() 是两种不同的类型。 A() 是一个返回 A 且不带参数的函数。

最终代码应该是

#include <type_traits>
#include <memory>

using namespace std;

template<typename T, typename Func>
void Example(Func)
{
// func can have params, or have no params
typename std::invoke_result<Func>::type result;
static_assert(std::is_same<decltype(result), std::unique_ptr<T> >::value, "Not a unique pointer to type 'T'"); // can't get this correct (tried different variations, looked over stackoverflow, etc.)
}

class Foo
{
public:
Foo(int i)
: My_I(i)
{
}

Foo()
: My_I(99)
{
}

~Foo() = default;

int My_I;
};

unique_ptr<Foo> CreateWithInt()
{
return make_unique<Foo>(11);
}

int main()
{
Example<Foo>(&CreateWithInt);
return 0;
}

关于模板函数参数的 C++17 invoke_result 和 static_assert,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51176572/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com