在C++ 11中,你仍然需要使用std::localtime和std::gmtime作为间接来打印std::chrono::time_point.这些函数在C++ 11引入的多线程环境中使用是不安全的,因为它们返回一个指向内部静态结构的指针.这尤其令人讨厌,因为C++ 11引入了方便的功能std::put_time,由于同样的原因几乎无法使用.
为什么这么根本被打破或者我忽略了什么?
似乎只能在别名模板的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
根据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) 假设我使用基于范围的循环编程时的当前规则说
使用
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:...),并且只在需要时不使用引用.
以下代码无法在最近的编译器上构建(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.
请参阅以下代码
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认为A是using base_type = A指默认构造函数A.msvc和clang似乎没有.
也许编译错误是由于继承触发的名称注入(因为修改using base_type = A为using base_type = ::A使所有编译器工作正常),但我想知道这个奇怪的错误是否是标准所说的.
更具体地说,
A::type,A只是一个类名(虽然gcc误解为一个函数名),它不是引入C 内部 A也不引入B.为什么这个名字被认为是私人的B?我正在尝试编写一个可以将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) 我已经看到过使用这个元函数,但从未真正理解为什么以及在什么情况下需要它.有人能用一个例子解释一下吗?
template <typename T>
struct identity
{
using type = T;
};
Run Code Online (Sandbox Code Playgroud) 标准是否允许以下内容?
#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)
c++ ×10
c++11 ×7
auto ×1
c++-chrono ×1
c++14 ×1
clang ×1
dictionary ×1
extern ×1
for-loop ×1
lambda ×1
libc++ ×1
name-lookup ×1
projection ×1
string-view ×1
templates ×1
visual-c++ ×1