不明白为什么return std::set<int>();还给空了std::set<int>.是否有运营商超载的operator ()在std::set上课吗?我假设它std::set<int>()是一个函数,而不是一个对象!这个函数定义在哪里?
似乎默认构造函数std::set<int> s;与表达式相同std::set<int>()???
谢谢你的回复...似乎我不懂C++基础知识......
我有两个智能指针:
Foo *p1 = new Foo;
Foo *p2 = new Foo;
std::unique_ptr<Foo> u1(p1);
std::unique_ptr<Foo> u2(p2);
Run Code Online (Sandbox Code Playgroud)
现在我想u1拥有p2.而我想u2拥有任何东西(或nullptr).对于cource,同时p1必须优雅地删除.
我应该用什么C++代码来完成它?
我知道C和C++中的每个文字都会获得特定的类型信息.我在C中编写了这个小程序,并在Visual Studio 2012中对其进行了编译.源文件名为'main.c'.
#include <stdio.h>
int main()
{
printf("sizeof(char) = %d\n",sizeof(char));
printf("sizeof('i') = %d",sizeof('i'));
getchar();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:
sizeof(char) = 1
sizeof('i') = 4
Run Code Online (Sandbox Code Playgroud)
我想知道角色的大小不是1字节.我将源文件重命名为'main.cpp',现在sizeof('a')返回1,如前所述.所以必须有语言特定的差异.为什么C 4字节中的字符大小而不是1?
只是测试/学习用C++重写|运算符来编写管道,以下程序无法编译:
忽略二进制表达式候选模板的无效操作数:...无法将'bitset'与'vector'匹配
似乎编译器正在尝试使用标准| 定义.该代码使用显式调用和显式类型参数集.
// g++ -std=c++11 Pipeline.cpp
#include <iostream>
#include <vector>
// ..............................................................
// ..............................................................
std::string dup (std::string s) {
return s + s;
}
// ..............................................................
// ..............................................................
template <typename TI, typename TO>
std::vector<TO> operator | (const std::vector<TI> & in, std::function<TO(TI)> f) {
std::vector<TO> out;
for (auto i : in) {
out.push_back ( f(i) );
}
return out;
}
// ..............................................................
// ..............................................................
int main () {
std::cout << " hello " << std::endl;
std::vector<std::string> vs …Run Code Online (Sandbox Code Playgroud) 我对ctor明确违约时会发生什么感到困惑.
这两个代码示例是否相同?
Y能够使用第一个选项是否有任何限制?
class X
{
public:
X() = default;
private:
Y m_y;
};
class X
{
public:
X() : m_y() {}
private:
Y m_y;
};
Run Code Online (Sandbox Code Playgroud) 我试图std::array在c ++中创建一个n维数组模板类(作为c ++数组的包装器),为整个n维数组分配一个数组块(避免使用n个数组和n个索引的开销).
在这样做时,我希望我的模板采用以下格式,sizes表示每个维度的大小.
template <class Type, size_t... sizes>
class array_dimensional{
private:
std::array<Type, /*here is the problem, how do
I get the product of all the sizes?*/> allocated_array;
...
Run Code Online (Sandbox Code Playgroud)
我的问题是我不知道如何获得所有尺寸的产品.
有可能这样做,如果是这样的话怎么样?
我有一个过滤器类,它有两个模板参数,输入数和输出数.
template<int Ins, int Outs>
class Filter
{
// implementation
};
Run Code Online (Sandbox Code Playgroud)
有时我需要串联多个过滤器,所以我想把它们包装在一个类中
template<int... args>
class Chain
{
};
Run Code Online (Sandbox Code Playgroud)
这样当我使用链条时
Chain<5, 10, 25, 15> chain;
Run Code Online (Sandbox Code Playgroud)
它将args展开成一个元组,最终在Chain类中得到类似的结果
std::tuple<Filter<5, 10>, Fiter<10, 25>, Filter<25, 15>> filters;
Run Code Online (Sandbox Code Playgroud)
这样的事情可能吗?我对这些概念很陌生,无法绕过它.
为什么这段代码会产生编译错误?
std::find_if(std::begin(some_list), std::end(some_list), [](const auto& item){
//some code
});
Run Code Online (Sandbox Code Playgroud)
当然在"自动"的错误?为什么不能自动知道类型?谢谢
我有这个简单的代码片段,它包含struct timespec并添加静态成员的最小值和最大值.
#include <sys/stat.h>
#include <limits>
struct Timespec : public timespec {
Timespec() :timespec() {};
Timespec(decltype(tv_sec) s,
decltype(tv_nsec) ns
) {
tv_sec = s;
tv_nsec = ns;
}
static const Timespec max;
static const Timespec min;
};
const Timespec Timespec::min = Timespec(0,0);
const Timespec Timespec::max = Timespec(
std::numeric_limits<decltype((timespec().tv_sec))>::max(),
std::numeric_limits<decltype((timespec().tv_nsec))>::max()
);
Run Code Online (Sandbox Code Playgroud)
它编译OK,但如果我更换decltype((timespec()/*...*/与
decltype((Timespec()/*...*/在年底的两行,我得到:
$ make timespec.o
g++ -std=c++0x -c -o timespec.o timespec.cc
In file included from timespec.cc:2:0:
/usr/include/c++/4.8/limits: In instantiation of ‘static constexpr _Tp std::numeric_limits<_Tp>::max() [with …Run Code Online (Sandbox Code Playgroud) 我有一个8个线程之间的共享哈希表(我是一个8核PC),每个线程在哈希表中读写.
在示例1中,我使用了经典的互斥锁,并且在示例2中所有8个核心都是100%我使用了shared_timed_mutex,因为读取访问可以在竞争中但是所有8个核心都在40%
问题出在哪儿?
example 1:
mutex mutex_hash;
-- thread --
mutex_hash.lock();
//read
mutex_hash.unlock();
..
mutex_hash.lock();
//write
mutex_hash.unlock();
Run Code Online (Sandbox Code Playgroud)
============================
example 2:
shared_timed_mutex mutex_hash;
-- thread --
mutex_hash.lock_shared();
//read
mutex_hash.unlock_shared();
..
mutex_hash.lock();
//write
mutex_hash.unlock();
Run Code Online (Sandbox Code Playgroud)