关于这个问题的讨论是在这个非常简单的问题的答案下开始的。
\n这个简单的代码具有意外的构造函数重载解析std::basic_string:
#include <string>\n\nint main() {\n std::string s{"some string to test", 2, 3};\n return 0;\n}\nRun Code Online (Sandbox Code Playgroud)\n现在有人期望这会调用这个构造函数:
\nstd::basic_string<CharT,Traits,Allocator>::basic_string - cppreference.com
\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
template<class T> basic_string( const T& t, size_type pos, size_type n, const Allocator& alloc = Allocator() ); (自 C++17 起) (11) template< class T > constexpr basic_string( const T& t, size_type pos, size_type const Allocator& alloc = Allocator() ); (自 C++20 起) (11)
基本原理基于此 C++ …
我正在寻找一个可迭代i和大小的函数,n并产生长度n为连续值的元组i:
x = [1,2,3,4,5,6,7,8,9,0]
[z for z in TheFunc(x,3)]
Run Code Online (Sandbox Code Playgroud)
给
[(1,2,3),(4,5,6),(7,8,9),(0)]
Run Code Online (Sandbox Code Playgroud)
标准库中是否存在这样的功能?
如果它作为标准库的一部分存在,我似乎无法找到它并且我已经没有用于搜索的术语.我可以自己写,但我宁愿不写.
以下代码与VS2010打印0,与我的期望相反:
#include <complex>
#include <iostream>
using namespace std;
int main(void)
{
complex<int> z(20, 200);
cout << abs<int>(z) << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它的类型是正常的double.
我有一个multimap,我想让它中的所有唯一键存储在一个向量中.
multimap<char,int> mymm;
multimap<char,int>::iterator it;
char c;
mymm.insert(pair<char,int>('x',50));
mymm.insert(pair<char,int>('y',100));
mymm.insert(pair<char,int>('y',150));
mymm.insert(pair<char,int>('y',200));
mymm.insert(pair<char,int>('z',250));
mymm.insert(pair<char,int>('z',300));
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?有方法可以使用键计算元素数,但不计算多图中唯一键的数量.
补充:通过唯一我指的是multimap中的所有键一次 - 它们可以在multimap中重复或出现一次.
所以这里的唯一键是-x,y和z
我意识到"为什么事情就像他们一样"问题通常不是最好的,但是很多人都在调整标准委员会讨论,所以我希望这可以在事实上得到回答,因为我对于什么是合法的好奇答案是.
基本上,我std::result_of第一次看到它时花了很长时间才弄清楚模板签名是怎么回事:我认为这是一个我从未见过的模板参数的全新构造.
template< class F, class... ArgTypes >
class result_of<F(ArgTypes...)>;
Run Code Online (Sandbox Code Playgroud)
经过一段时间的思考,我意识到这实际上是什么:F(ArgTypes...)是一个函数类型,但它不是正在评估结果类型的函数的类型(只是F):它是一个带ArgTypes...参数和返回类型的函数的类型F.
这不是......奇怪吗?有点hackish?有没有人知道委员会是否讨论过任何替代方案,例如,以下......
template< class F, class... ArgTypes >
class result_of<F, ArgTypes...>;
Run Code Online (Sandbox Code Playgroud)
?
我想有可能的情况是第二个结构不能像第一个结构那样容易使用,但是哪个?
我不是试图对此作出判断,但只是在我第一次看到它时,这对我来说是合法的混淆,所以我很好奇是否有充分的理由.我意识到答案的一部分可能只是"因为Boost这样做了",但仍留下剩下的(事实)问题......
是否存在技术原因Boost选择此语法来编码类型信息而不是任何替代方案?
C++ 11委员会是否有任何关于如何将其标准化的讨论,因为无论如何std::result_of都可以decltype相当容易地实现?
我有一个untemplated仿函数对象,我试图存储为std::function另一个对象内部.这个对象非常重量级,因此它被标记为不可复制,但它确实有一个移动构造函数.但是,尝试从临时构造函数构造std :: function或分配它失败.
这是引发错误的最小示例.
// pretend this is a really heavyweight functor that can't be copied.
struct ExampleTest
{
int x;
int operator()(void) const {return x*2;}
ExampleTest( ) :x(0){}
ExampleTest( int a ) :x(a){}
// allow move
ExampleTest( ExampleTest &&other ) :x(other.x) {};
private: // disallow copy, assignment
ExampleTest( const ExampleTest &other );
void operator=( const ExampleTest &other );
};
// this sometimes stores really big functors and other times stores tiny lambdas.
struct ExampleContainer
{
ExampleContainer( int …Run Code Online (Sandbox Code Playgroud) 标准库将std :: hash实现为专门用于不同类型的模板结构.它是这样使用的:
#include <iostream>
#include <functional>
int main()
{
std::hash<int> hasher;
std::cout << hasher(1337) << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我的问题是这种设计选择背后的原因是什么.为什么它没有实现为模板函数并像这样使用:
#include <iostream>
#include <functional>
int main()
{
std::cout << std::hash<int>(1337) << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我正在使用Eclipse和CDT插件来开发C++.我也使用std库来创建向量,我在调试时遇到问题:Eclipse不允许我查看向量的内容.
有没有办法能够正确调试它?
我有一个std::array<Foo, 10>,我想一个创建std::array<Bar, 10>使用函数从Foo到Bar.通常我会这样使用std::transform:
array<Bar, 10> bars;
transform(foos.begin(), foos.end(), bars.begin(), [](Foo foo){
return Bar(foo.m_1, foo.m_2);
});
Run Code Online (Sandbox Code Playgroud)
但是,Bar没有默认构造函数,所以我无法创建bars数组.我总是可以使用,vector但能够array用来保证我总是有10个元素会很好.那可能吗?
让我举一个具体的例子来说明我的意思.
我有两个C++标准草案:N4296现在很老了,最近的修订版N4750.我感兴趣的是一些小节,例如[unord.hash].版本N4296需要从std::hash提供两个嵌套类型argument_type和result_type,但这一要求不再存在于N4750.
如何找到修订版本,删除此要求的动机及其动机?