相关疑难解决方法(0)

trait从成员函数类型中删除const?

Tdouble(float)const我得到这个错误,当我尝试使用function<T>.

implicit instantiation of undefined template 'std::function<double (float) const>'
Run Code Online (Sandbox Code Playgroud)

但是什么时候T可以double(float).我试图用它std:: remove_cv<T>::type来删除它const,但这不起作用.是的,我有#include<functional>.

所以我的主要问题是:如何修复此问题并删除,const以便我可以将此函数类型放入std:: function.


我在使用operator()lambdas方法时遇到过这个问题,但我认为这个问题通常是关于任何方法类型的,而不仅仅是对于lambdas


但我的第二个问题是:double(float)const甚至意味着什么?!! 我能够了解

double (ClassName::) (float) const
Run Code Online (Sandbox Code Playgroud)

因为这意味着成员函数无法修改其ClassName对象.当我将此类型放入模板中以删除类类型时,我得到的double(float)const是导致问题的.

template<typename>
struct DropClassType;
template<typename Sig, typename C>
struct DropClassType<Sig (C::*)> {
  typedef Sig type_without_class;
};
Run Code Online (Sandbox Code Playgroud)

(clang 3.4.2.来自g ++ - 4.9.1的错误更加神秘,但基本相同)

c++ templates const

9
推荐指数
2
解决办法
1196
查看次数

在std :: function上强制执行"noexcept"?

这段代码编译并运行,抛出int:

#include <functional>

void r( std::function<void() noexcept> f ) { f(); }

void foo() { throw 1; }

int main()
{
    r(foo);
}
Run Code Online (Sandbox Code Playgroud)

但是我希望编译器拒绝该行,r(foo);因为r只应传递一个noexcept函数.该noexcept说明符似乎被忽略.有没有办法实现这一目标?

编辑:这个问题是不同的是关于传递函数指针时应该转发的noexcept-ness的知识?因为我要求补救,特别是在这种情况下std::function.

c++ function noexcept

9
推荐指数
1
解决办法
1376
查看次数

标签 统计

c++ ×2

const ×1

function ×1

noexcept ×1

templates ×1