小编Yak*_*ont的帖子

这个可变参数模板代码有什么作用?

template <class F, class... Args> 
void for_each_argument(F f, Args&&... args) { 
    [](...){}((f(std::forward<Args>(args)), 0)...); 
}
Run Code Online (Sandbox Code Playgroud)

它最近在isocpp.org上有特色,没有任何解释.

c++ templates variadic-templates c++11

16
推荐指数
2
解决办法
1303
查看次数

为什么std :: array :: size constexpr具有简单类型(int,double,...)但不是std :: vector(GCC)?

以下代码:

std::array<int, 4> arr1;
std::array<float, arr1.size()> arr2;
Run Code Online (Sandbox Code Playgroud)

...编译两者gccclang因为std::array::size被考虑constexpr.

但是以下gcc版本没有编译(版本5.3.0 20151204):

std::array<std::vector<int>, 4> arr1;
std::array<std::vector<double>, arr1.size()> arr2;
Run Code Online (Sandbox Code Playgroud)

对我来说,没有理由这样的代码如果第一个有效则无法编译,但由于我没有找到很多帖子,我不知道它是一个gccbug还是一个clang扩展?

错误来自gcc(我真的不明白......):

main.cpp: In function 'int main()':
main.cpp:6:46: error: call to non-constexpr function 'constexpr std::array<_Tp, _Nm>::size_type std::array<_Tp, _Nm>::size() const [with _Tp = std::vector<int>; long unsigned int _Nm = 4ul; std::array<_Tp, _Nm>::size_type = long unsigned int]'
     std::array<std::vector<double>, arr1.size()> arr2;
                                              ^
In file included from main.cpp:1:0:
/usr/local/include/c++/5.3.0/array:170:7: note: 'constexpr std::array<_Tp, …
Run Code Online (Sandbox Code Playgroud)

c++ arrays constexpr c++11

16
推荐指数
1
解决办法
1429
查看次数

关于捕获异常良好做法

我正在用C++ 11编写一个小程序,并且第一次真正使用异常.

我有一个关于如何有效地捕获异常的问题,经过一些谷歌搜索我仍然没有答案.

这是一个问题:通过(const?)左值引用或(const?)右值引用捕获异常之间的效率(或推荐)是什么?

在代码中,这给:

1)

try { throw std::exception{"what"}; }
catch (std::exception& ex) {}
Run Code Online (Sandbox Code Playgroud)

2)

try { throw std::exception{"what"}; }
catch (const std::exception& ex) {}
Run Code Online (Sandbox Code Playgroud)

3)

try { throw std::exception{"what"}; }
catch (std::exception&& ex) {}
Run Code Online (Sandbox Code Playgroud)

4)

try { throw std::exception{"what"}; }
catch (const std::exception&& ex) {}
Run Code Online (Sandbox Code Playgroud)

c++ exception try-catch rvalue-reference c++11

15
推荐指数
1
解决办法
2897
查看次数

SFINAE可以检测到私人访问违规吗?

我想知道如果我测试一个班级的某个成员并且该成员是私人的,那么sfinae会做出什么反应?它是否会出错或者说是好的还是会以错误的方式出错?

c++ sfinae language-lawyer

15
推荐指数
2
解决办法
937
查看次数

用于检查类型相等性的编译时函数

我需要实现自包含的编译时函数来检查类型相等(没有参数的函数模板bool eqTypes<T,S>()).

自包含意味着不依赖于图书馆.

我对这一切并不擅长.这就是我尝试过的,但这不是我需要的.

template<typename T>
bool eq_types(T const&, T const&) { 
return true;
}

template<typename T, typename U> 
bool eq_types(T const&, U const&) { 
return false; 
}
Run Code Online (Sandbox Code Playgroud)

c++ templates compile-time

15
推荐指数
1
解决办法
1万
查看次数

为什么C++ 11没有隐式地将lambda转换为std :: function对象?

我实现了一个通用事件发射器类,它允许代码注册回调,并使用参数发出事件.我使用Boost.Any类型擦除来存储回调,以便它们可以具有任意参数签名.

这一切都有效,但由于某种原因,传入的lambdas必须首先变成std::function对象.为什么编译器不推断lambda是函数类型?是因为我使用可变参数模板的方式吗?

我使用Clang(版本字符串:) Apple LLVM version 5.0 (clang-500.2.79) (based on LLVM 3.3svn).

码:

#include <functional>
#include <iostream>
#include <map>
#include <string>
#include <vector>

#include <boost/any.hpp>


using std::cout;
using std::endl;
using std::function;
using std::map;
using std::string;
using std::vector;


class emitter {

   public:

      template <typename... Args>
      void on(string const& event_type, function<void (Args...)> const& f) {
         _listeners[event_type].push_back(f);
      }

      template <typename... Args>
      void emit(string const& event_type, Args... args) {
         auto listeners = _listeners.find(event_type);
         for (auto l : listeners->second) {
            auto lf …
Run Code Online (Sandbox Code Playgroud)

c++ lambda templates template-function c++11

15
推荐指数
2
解决办法
2276
查看次数

我可以在std :: function模板签名类型参数中使用命名参数吗?

我可以合法地在std::function(或其他类似的构造)中使用模板参数的名称吗?例如给出代码

std::function<int(int, int)> some_func;
Run Code Online (Sandbox Code Playgroud)

我可以按照以下方式重写吗?

std::function<int(int begin, int end)> some_func;
Run Code Online (Sandbox Code Playgroud)

如果它符合标准,那将是非常好的,因为单独的类型几乎没有关于参数目的的信息.

到目前为止,我已经在Visual Studio 2013中对它进行了测试,但它确实有效.编辑:它也适用于gcc 4.8.1.

c++ c++11 std-function

15
推荐指数
2
解决办法
1093
查看次数

C++相当于Rust的Result <T,E>类型?

我喜欢在我的C++代码中使用std :: experimental :: optional,但问题是value_or要求默认值与可选的值相同.

当我想要一个包含int或包含错误消息的可选项时,这不能很好地工作.

我想我可以使用一个带有布尔值的联合结构来指示该值是否存在或者它是一个错误,但如果C++只有Result<T, E>类似Rust 的类型,那肯定会很好.

有这样的类型吗?Boost为什么没有实现呢?

结果确实比Option更有用,Boost的人们肯定知道它的存在.也许我会去阅读Rust实现,然后将其复制到C++?

例如:

// Function either returns a file descriptor for a listening socket or fails
// and returns a nullopt value.
// My issue: error messages are distributed via perror.
std::experimental::optional<int> get_tcp_listener(const char *ip_and_port);
// You can use value_or to handle error, but the error message isn't included!
// I have to write my own error logger that is contained within
// get_tcp_listener. I would really appreciate if …
Run Code Online (Sandbox Code Playgroud)

c++ boost optional c++17

15
推荐指数
3
解决办法
5052
查看次数

我们如何测试是否可以使用prvalue调用某个类型的表达式?

使用我们看到了新的is_invocable和花哨的新prvalues,它们并不是真正的价值.

这允许您创建一个对象而无需首先在逻辑上构造它,然后忽略构造.

我遇到了一个问题,std::is_invocable用于测试你是否可以调用某些内容,而prvalue规则似乎会发生冲突:

struct no_move {
  no_move(no_move&&)=delete;
  explicit no_move(int) {}
};
void f( no_move ) {}
Run Code Online (Sandbox Code Playgroud)

现在我们可以问一下是否f可以使用类型的prvalue调用no_move

f( no_move(1) )
Run Code Online (Sandbox Code Playgroud)

std::is_invocable< decltype(&f), no_move >不起作用,因为它使用的std::declval<no_move>()是xvalue,no_move&&而不是类型的prvalue no_move.

这是相同的,但保证省略使得一些函数可以用xvalue(即" T&&")调用,而其他函数可以用prvalues类型调用T.

有替代方案,还是我们必须发明自己的特性来处理这种情况?

(在一个理论的世界里std::declval<T>返回T,而不是T&&,is_invocable会的,我相信,做正确的事).

c++ c++17 prvalue declval invocable

15
推荐指数
2
解决办法
477
查看次数

构造函数继承的虚拟继承

我有一个类层次结构,归结为

class Module { };

struct Port {
    Module& owner;
    Port(Module& owner) : owner(owner) {}
};

struct InPort    : virtual Port    { using Port::Port; };
struct OutPort   : virtual Port    { using Port::Port; };
struct InOutPort : InPort, OutPort { using Port::Port; };
Run Code Online (Sandbox Code Playgroud)

如您所见,我更愿意创建一些基本功能,并以经典的菱形图案继承它.我也想使用构造函数继承使其尽可能地证明未来...

但是,这不起作用如上所述

prog.cpp: In function 'int main()':
prog.cpp:14:15: error: use of deleted function 'InOutPort::InOutPort(Module&)'
  InOutPort p(m);
Run Code Online (Sandbox Code Playgroud)

InOutPort使用更明确的版本替换定义也是不够的:

struct InOutPort : InPort, OutPort { InOutPort(Module& m) : Port(m), InPort(m), OutPort(m) { } }; …
Run Code Online (Sandbox Code Playgroud)

c++ inheritance virtual-inheritance c++11

14
推荐指数
1
解决办法
311
查看次数