超载模板功能的constexpr别名
Constexpr alias of overloaded template function
试图在该类的特定构造函数的特定类类型上求和。我最好的尝试:
class foo { public: foo(int x) : y(x) {} int y; };
constexpr auto newfoo = static_cast<std::shared_ptr<foo>(*)(int)>(std::make_shared<foo>);
屈服:
error: invalid static_cast from type ‘<unresolved overloaded function type>’ to type ‘std::shared_ptr<foo> (*)(int)’
constexpr auto newfoo = static_cast<std::shared_ptr<foo>(*)(int)>(std::make_shared<foo>);
我在做什么错?
std::make_shared
是 variadic函数模板。您仅将<foo>
指定为模板参数,但是您也需要在其中某个地方进行int
。无论如何,您的方法一定会失败,因为它依赖于make_shared
的模板参数的布局,并且因为在C 中使用过载集通常很麻烦。
我建议的是创建包装器功能:
constexpr auto newfoo(int x)
{
return std::make_shared<foo>(x);
}
我认为,编写,阅读和理解更容易。如果您确实需要Sfinae-Fricrignliness和noexcept
,则可以重复三遍身体:
constexpr auto newfoo(int x)
-> decltype(std::make_shared<foo>(x))
noexcept(noexcept(std::make_shared<foo>(x)))
{ return std::make_shared<foo>(x); }
可以使用宏来使上述声明减轻痛苦。
如果您真的想要一个功能指针,这似乎有效:
auto newfoo =
static_cast<std::shared_ptr<foo>(*)(const int&)>(
&std::make_shared<foo, const int&>);
查看make_shared
的声明:
template< class T, class... Args >
shared_ptr<T> make_shared( Args&&... args );
您需要为T=foo
提供CC_7。由于Args...
是一个转发参考包,因此它始终将推导为 lvalue参考或 rvalue参考。这就是为什么<foo, const int&>
是有效的模板参数集,而<foo, int>
不是。
正如Zefick在评论中指出的那样,所有这些都可以简化为:
constexpr auto newfoo = &std::make_shared<foo, const int&>;
在这里真的不需要演员。
相关文章:
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 部分定义/别名模板模板参数
- 多成员Constexpr结构初始化
- 如何在C++20中创建模板别名的推导指南
- 条件constexpr函数
- constexpr 函数中的非文字(通过 std::is_constant_evaluated)
- Visual C++ constexpr Hints
- 如何确认我的constexpr表达式实际上已经在编译时执行
- 为什么constexpr的性能比正常表达式差
- 是否可以使用if constexpr删除控制流语句
- 要与"if constexpr"一起使用的编译时消息(在预处理器之后)
- 为什么std::isnan 不是 constexpr?
- Constexpr替代了新的放置方式,可以让内存中的对象保持未初始化状态
- 当一个值是非常量但用常量表达式初始化时使用constexpr
- 更多constexpr容器是否需要mark_immutable_if_consexpr
- C++从其他 constexpr 创建 lambda 不能按顺序执行 Constexpr
- "constexpr if"不支持别名使用?
- 超载模板功能的constexpr别名
- 是否可以将类型别名定义为constexpr函数
- C++:使用constexpr对模板参数进行别名,导致函数定义与声明不匹配