- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
为了避免重复相似的函数定义,我使用了这样一个事实,即具有相同名称的继承成员是隐藏的,并且可以通过使用特定的基类限定符来选择/区分:
#include <type_traits>
template<template<class, class> class CreatePolicy, class Base, class... Derived>
class factory : public CreatePolicy<Base, Derived>...
{
public:
template
<
class T,
std::enable_if_t
<
!std::is_same<Base, T>::value && std::is_base_of<Base, T>::value, int
> = 0
>
std::unique_ptr<Base> create() noexcept( noexcept( CreatePolicy<Base, T>::create() ) )
{
return CreatePolicy<Base, T>::create();
}
};
使用它看起来像这样:
#include <memory>
#include <cassert>
template<class Base, class Derived>
struct create_t
{
auto create()
{
return std::make_unique<Derived>();
}
};
struct base
{
virtual void print() const noexcept
{
std::cout << "base\n";
}
};
struct derived : public base
{
virtual void print() const noexcept override
{
std::cout << "derived\n";
}
};
int main()
{
factory<create_t, base, derived> f;
auto d = f.create<derived>();
d->print();
}
虽然在 VC++2015 下编译,但由于 create()
的 noexcept
规范,GCC5.1 给我这个错误:
..\main.cpp|20|error: cannot call member function 'auto
create_t<T>::create() [with T = derived]' without object|
如何编译?
最佳答案
语法 base_class::method(...)
调用基类的成员函数只在成员函数体内有效。这特别排除了未评估的上下文,例如它们出现在 noexcept
规范中。然而,这些上下文的美妙之处在于它们就是:未评估。所以你可以以某种方式获取你的类的一个实例并调用它的方法。执行此操作的标准方法是使用 std::declval
:
std::unique_ptr<Base> create() noexcept( noexcept( std::declval<CreatePolicy<Base, T>&>().create() ) )
关于c++ - 当我从我的继承列表中选择一个特定的隐藏函数时,我如何使用 noexcept?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38295607/
考虑: class test { private: int n; int impl () const noexcept { return
#include class A { std::vector vec; void swap( A & other) noexcept(noexcept(vec.swap(other.
在 the C++ standard有如下定义: template void swap(T (&a)[N], T (&b)[N]) noexcept(noexcept(swap(*a,
从 c++20 开始,我们可以使用 consteval 说明符定义立即数函数。当一个函数被声明为 consteval 时,对该函数的每次调用都必须产生一个编译时常量,否则该程序就是病式的。此外,由于
为什么 noexcept 运算符采用表达式而不是函数签名/声明? 考虑以下虚拟示例: #include void strProcessor(const std::string& str) noexc
我有一段通用代码,其性能对我来说很重要,因为我面临着与用 C 编写的著名手工代码的运行时间相匹配的挑战。在开始使用 noexcept 之前,我的代码运行时间为 4.8 秒。通过将 noexcept 放
如果我将函数标记为 noexcept(false),或任何其他计算结果为 false 的表达式,这意味着什么? (1) 我是否向编译器保证该函数可以抛出异常?,(2) 还是我不保证它是否可以抛出异常?
我们遇到过这种情况,想知道解决它的最佳方法 template struct A : T { A(T &&t) noexcept(noexcept(T(std::move(t)))) :T
此代码使用 gcc 4.8.2 (-std=c++11) 编译失败,但使用 clang 3.4 (trunk) (-std=c++11) 编译: #include #include struct
当函数标记为 noexcept 时,GCC 或 Clang 中是否有一个标志会抛出编译时错误(或警告)尝试调用未标记为 noexcept 的函数? 如果不是,那么当您删除 noexcept 时,您应该
假设我有一个类 class C : public B { public: C() noexcept; } noexcept 说明符是否需要基类的相同 promise ?也就是说,当我考虑使
长标题:为什么 std:variant 的 operator=(T&& t) 的 noexcept 规范不依赖于内部类型的析构函数的 noexcept 规范? 我可以在 cppreference 上看
在下面的代码中,我尝试对函数使用条件异常规范,但编译失败,尽管如果在函数外部使用它就可以了。 void may_throw(); // ERROR: expression must have bool
目前在C++这些都不可能,编译器提示它需要一个表达式。 这对我来说似乎微不足道,如果您正在构建一个具有可变数量类型的类似元组的对象,如何检查所有这些类型是否都是 nothrow default/mov
在 The C++ Programming Language 一书中写道,您可以将函数声明为有条件的 noexcept .例如: template void my_fct(T& x) noexcept
根据this question的回答,默认移动构造函数可以定义为 noexcept在一定条件下。例如,下面的类生成一个 noexcept移动构造函数: class C {}; 根据对this ques
拿这个代码: template void my_func() { T::some_method(); } int main() { std::cout ()) ? "noexcept" :
这个声明没问题: void memberFunction(T& functor, double value)noexcept(noexcept(functor(value))); 对于一个 templ
我在思考 noexcept 时遇到了一些麻烦。 template int pop(int idx) noexcept(noexcept(SIZE > 0)) // this is what I do
为什么 std::decay 不从函数指针中移除 noexcept 说明符? 例如这符合 c++17 #include template struct is_noexcept {}; templat
我是一名优秀的程序员,十分优秀!