小编Pio*_*cki的帖子

为什么std :: localtime和std :: gmtime没有C++ 11线程安全替代品?

在C++ 11中,你仍然需要使用std::localtimestd::gmtime作为间接来打印std::chrono::time_point.这些函数在C++ 11引入的多线程环境中使用是不安全的,因为它们返回一个指向内部静态结构的指针.这尤其令人讨厌,因为C++ 11引入了方便的功能std::put_time,由于同样的原因几乎无法使用.

为什么这么根本被打破或者我忽略了什么?

c++ thread-safety c++11 c++-chrono

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

别名模板的包扩展

似乎只能在别名模板的pack参数的位置扩展pack参数.对于类或函数模板,情况并非如此:

template <class T, class... Args> struct x { using type = T; };

template <class T, class... Args> using x_t     = typename x<T, Args...>::type;
template <class... Args>          using x_fix_t = typename x<Args...>::type;

template <class... Args> auto f(Args...) -> void {
  typename x<Args...>::type v1; // OK
  x_t<Args...> v2; // Error
  x_fix_t<Args...> v3; // OK
}
Run Code Online (Sandbox Code Playgroud)

更简单的情况:

template <class T, class U> using y_t = T;

template <class... Args> auto f(Args...) -> void {
  y_t<Args...> v4; // Error
} …
Run Code Online (Sandbox Code Playgroud)

c++ language-lawyer variadic-templates c++11 template-aliases

40
推荐指数
1
解决办法
1812
查看次数

C++ Lambdas:捕获列表与参数列表

根据C++ 11标准,lambda表达式可以通过捕获列表,参数列表或两者使用封闭范围中的变量.

那么,让我们看看相同代码的两个版本.

1)捕获

int x = 4;

cout << "With capture  : Factorial of " << x << " = " << [x]() // <= Capture
{
    int r = 1;
    for (int i = x; i > 1; i--) r = r * i;
    return r;
}() << endl;
Run Code Online (Sandbox Code Playgroud)

2)带参数

int x = 4;

cout << "With parameter: Factorial of " << x << " = " << [](int x) // <= Parameter
{
    int r = 1; …
Run Code Online (Sandbox Code Playgroud)

c++ lambda c++11

31
推荐指数
2
解决办法
6633
查看次数

什么`auto && e`在基于范围的for循环中做什么?

假设我使用基于范围的循环编程时的当前规则说

使用for(auto const &e :...)for(auto &e:...)在可能时使用for(auto a: ...).

我以我自己的经验和这个问题为例.

但是在读完关于循环的新简洁之后我想知道,我不应该用我&的规则替换我的规则&&吗?正如这里所写,这看起来像迈耶斯的通用参考.

所以,我问自己,如果我的新规则要么

使用for(auto const &&e :...)for(auto &&e:...)在可能的时候......

或者这不总是有效,因此应该是相当复杂的

检查for(auto const &&e :...)或者for(auto &&e:...)是可能的,再考虑for(auto const &e :...)或者for(auto &e:...),并且只在需要时不使用引用.

c++ for-loop c++11 universal-reference forwarding-reference

28
推荐指数
1
解决办法
9812
查看次数

使用string_view进行地图查找

以下代码无法在最近的编译器上构建(g ++ - 5.3,clang ++ - 3.7).

#include <map>
#include <functional>
#include <experimental/string_view>

void f()
{
    using namespace std;
    using namespace std::experimental;
    map<string, int> m;
    string s = "foo";
    string_view sv(s);
    m.find(sv);
}
Run Code Online (Sandbox Code Playgroud)

clang返回的错误:

error: no matching member function for call to 'find'
    m.find(sv);
    ~~^~~~
Run Code Online (Sandbox Code Playgroud)

但是不find应该能够使用类似的类型?Cppreference提到了以下重载:

template< class K > iterator find( const K& x );

发生同样的错误boost::string_ref.

c++ dictionary c++14 string-view

26
推荐指数
1
解决办法
4847
查看次数

使用声明的奇怪行为

请参阅以下代码

struct A { using type = int; };
struct B : private A {};
struct C : B { using base_type = A; };
Run Code Online (Sandbox Code Playgroud)

所有的gcc 6.1,clang 3.8和msvc 2015更新3都拒绝编译它,因为A它不是一个可访问的名称,C因为它A是一个私有基础B.似乎gcc认为Ausing base_type = A指默认构造函数A.msvc和clang似乎没有.

也许编译错误是由于继承触发的名称注入(因为修改using base_type = Ausing base_type = ::A使所有编译器工作正常),但我想知道这个奇怪的错误是否是标准所说的.

更具体地说,

  1. 据我所知,不是A::type,A只是一个类名(虽然gcc误解为一个函数名),它不是引入C 内部 A也不引入B.为什么这个名字被认为是私人的B
  2. 该编译错误是否应被视为错误,或者是标准规范的边缘情况?

c++ using-declaration private-inheritance name-lookup c++11

26
推荐指数
1
解决办法
1200
查看次数

如何从参数列表中推导出函数对象的返回类型?

我正在尝试编写一个可以将a vector<T>转换为a 的投影函数vector<R>.这是一个例子:

auto v = std::vector<int> {1, 2, 3, 4};
auto r1 = select(v, [](int e){return e*e; }); // {1, 4, 9, 16}
auto r2 = select(v, [](int e){return std::to_string(e); }); // {"1", "2", "3", "4"}
Run Code Online (Sandbox Code Playgroud)

第一次尝试:

template<typename T, typename R>
std::vector<R> select(std::vector<T> const & c, std::function<R(T)> s)
{
   std::vector<R> v;
   std::transform(std::begin(c), std::end(c), std::back_inserter(v), s);
   return v;
}
Run Code Online (Sandbox Code Playgroud)

但对于

auto r1 = select(v, [](int e){return e*e; });
Run Code Online (Sandbox Code Playgroud)

我明白了:

错误C2660:'select':函数不带2个参数

我必须明确地打电话select<int,int>去工作.我不喜欢这样,因为类型是多余的.

auto r1 = select<int, …
Run Code Online (Sandbox Code Playgroud)

c++ templates projection visual-c++ visual-c++-2013

20
推荐指数
2
解决办法
8272
查看次数

什么时候使用"身份"tmp技巧?

我已经看到过使用这个元函数,但从未真正理解为什么以及在什么情况下需要它.有人能用一个例子解释一下吗?

template <typename T>
struct identity
{
    using type = T;
};
Run Code Online (Sandbox Code Playgroud)

c++

18
推荐指数
2
解决办法
1320
查看次数

Clang的'type_visibility'属性做什么,什么时候应该使用它?

它在libc中使用++在很多模板类型,如tuple_element,tuple等.

据我所知,除了在clang项目中引入它和此单元测试提交消息之外,没有公开文档说明它的作用.

c++ clang c++11 libc++

17
推荐指数
1
解决办法
847
查看次数

可以将变量重新声明为推导为相同类型的auto吗?

标准是否允许以下​​内容?

#include <iostream>

extern int a;
auto a = 3;

int main(int, char**)
{
    std::cout << a << std::endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

clang接受代码. g ++抱怨声明冲突.

c++ extern language-lawyer auto c++11

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