在Stack Overflow问题中,在C++ 11中不允许重新定义lambda,为什么?,给出了一个不编译的小程序:
int main() {
auto test = []{};
test = []{};
}
Run Code Online (Sandbox Code Playgroud)
问题得到了回答,一切似乎都很好.然后是Johannes Schaub并做了一个有趣的观察:
如果你
+在第一个lambda之前放置一个,它会神奇地开始工作.
所以我很好奇:为什么以下工作呢?
int main() {
auto test = +[]{}; // Note the unary operator + before the lambda
test = []{};
}
Run Code Online (Sandbox Code Playgroud)
考虑以下程序:
int main ()
{
const int e = 10;
for (decltype(e) i{0}; i < e; ++i) {
// do something
}
}
Run Code Online (Sandbox Code Playgroud)
这无法使用clang(以及gcc)进行编译:
decltype.cpp:5:35: error: read-only variable is not assignable
for (decltype(e) i{0}; i < e; ++i) {
^ ~
Run Code Online (Sandbox Code Playgroud)
基本上,编译器假设i必须是const,因为e是.
有没有办法可以decltype用来获取类型e,但删除说明const符?
鉴于此std::array< T, 0 >,为什么它不是空的?我的意思是"空",如:
std::is_empty< std::array< int, 0 > >::value
Run Code Online (Sandbox Code Playgroud)
回来false和
#include <iostream>
#include <tuple>
#include <array>
struct Empty {};
int main()
{
std::cout << sizeof(std::tuple<int>) << std::endl;
std::cout << sizeof(std::tuple<int,Empty>) << std::endl;
std::cout << sizeof(std::tuple<int,std::array<int,0>>) << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
产量
4
4
8
Run Code Online (Sandbox Code Playgroud)
这意味着,std::array<int,0>没有应用空基础优化(EBO).
这对我来说似乎特别奇怪std::tuple<>(注意:没有模板参数)是空的,即std::is_empty<std::tuple<>>::value产量true.
问题:为什么这样,因为这个尺寸0已经是一个特例了std::array?这是标准的故意还是疏忽?
请考虑以下代码:
template< template< typename ... > class ... Ts >
struct unite
{
template< typename ... T >
struct type
: Ts< T ... > ...
{ };
};
// This does not work as ::type does not name a type, but a template:
// template< template< typename ... > class ... Ts >
// using unite_t = typename unite< Ts ... >::type;
template< typename > struct debug_none {};
template< typename > struct debug_cout {};
template< typename ... > struct …Run Code Online (Sandbox Code Playgroud) 假设您有一个类型变量,std::vector<std::string>并使用初始化列表初始化它:
using V = std::vector<std::string>;
V v = { "Hello", "little", "world", "of", "move", "semantics" };
Run Code Online (Sandbox Code Playgroud)
编译器将为std::string每个字符串文字创建一个临时文件,在这些文件上创建一个初始化列表,然后调用ctor V并创建向量.ctor不知道所有这些字符串都是临时字符串,所以它正在复制每个字符串.
我没有在标准中找到任何允许矢量ctor在临时时移动元素的东西.
我错过了什么或使用初始化程序列表导致不必要的副本?我正在编写类,这个问题可能会导致代码效率低下.任何避免不必要的副本的技术将不胜感激.
任意给定类型T的内置阵列X,也有功能std::begin()和std::end()我可以打电话,但为什么是不是有std::size()?似乎奇怪没有那个.
我可以使用std::end(x)-std::begin(x),但仍然std::size(x)会更好.
是的,我知道这些std::vector和std::array课程.这只是一个问题,为什么STL中还没有这样简单的东西.
给定一个C符合STL的容器类型,如何正确检测是否C包含成员函数reserve?我尝试了以下方法(使用GCC 4.6.3):
template< typename C, typename = void >
struct has_reserve
: std::false_type
{};
template< typename C >
struct has_reserve< C, typename std::enable_if<
std::is_same<
decltype( &C::reserve ),
void (C::*)( typename C::size_type )
>::value
>::type >
: std::true_type
{};
Run Code Online (Sandbox Code Playgroud)
这适用于C存在std::vector,但不适用于无序容器,例如std::unordered_set.其原因是,这reserve是一个的(直接)成员函数std::vector,但是对于无序的容器它是从基类继承的,即,它的签名是不void (C::*)( typename C::size_type )但void (B::*)( typename C::size_type )对于一些未指定的基类B的C.
我知道如何解决它并检测reserve即使是遗传,但它看起来很笨拙,我想知道标准允许什么.所以...
我的问题是:标准是允许reserve从未指定的基类继承还是概要绑定并需要直接成员函数?
为了fun and profit™,我正在用C++编写一个trie类(使用C++ 11标准.)
我trie<T>有一个迭代器,trie<T>::iterator.(它们实际上都是函数 const_iterator s,因为你无法修改trie value_type.)迭代器的类声明看起来像这样:
template<typename T>
class trie<T>::iterator : public std::iterator<std::bidirectional_iterator_tag, T> {
friend class trie<T>;
struct state {
state(const trie<T>* const node, const typename std::vector<std::pair<typename T::value_type, std::unique_ptr<trie<T>>>>::const_iterator& node_map_it ) :
node{node}, node_map_it{node_map_it} {}
// This pointer is to const data:
const trie<T>* node;
typename std::vector<std::pair<typename T::value_type, std::unique_ptr<trie<T>>>>::const_iterator node_map_it;
};
public:
typedef const T value_type;
iterator() =default;
iterator(const trie<T>* node) {
parents.emplace(node, node->children.cbegin());
// ...
}
// ...
private:
std::stack<state> …Run Code Online (Sandbox Code Playgroud) 我想验证以下是GCC中的错误,而不是我对C++的理解.请考虑以下代码:
struct A
{
struct B
{
template< typename U > U as() const { return U(); }
};
B operator[]( int ) const { return B(); }
};
template< typename T >
struct as
{
template< typename U >
static T call( const U& u )
{
return u[ 0 ].as< T >(); // accepted by Clang 3.2, rejected by GCC 4.7
// return u[ 0 ].template as< T >(); // does not help and is IMHO not needed …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用C++ 11初始化地图地图.我的编译器是VS 2013 Express.
unordered_map<EnumType, unordered_map<string, string>> substitutions = {
{
Record::BasementType,
{
{ "0", "" },
{ "1", "Slab or pier" },
{ "2", "Crawl" }
}
},
{
Record::BuildingStyle,
{
{ "0", "" },
{ "1", "Ranch" },
{ "2", "Raised ranch" }
}
},
// ... and so on
};
Run Code Online (Sandbox Code Playgroud)
它是编译但我在ntdll.dll中获得断点.但是这段代码的简化版本:
unordered_map<EnumType, unordered_map<string, string>> substitutions = {
{
Record::BasementType,
{
{ "0", "" },
{ "1", "Slab or pier" },
{ "2", "Crawl" }
}
},
// …Run Code Online (Sandbox Code Playgroud)