#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
void printArray(int* arr, int size) {
cout << "Printing the array..." << endl;
for (int index = 0; index < size; index++) {
cout << arr[index] << endl;
}
}
void populateArray(int* arr, int size) {
for (int index = 0; index < size; index++) {
arr[index] = index * 10 + 1;
}
}
int main() {
int size = 2;
int* arr = new int[size];
populateArray(arr, size);
size_t newSize …Run Code Online (Sandbox Code Playgroud) 我尝试在visual studio 2013(社区版)中编译一些代码,但我遇到了问题:以下代码拒绝编译.
struct X
{
X(double y);
};
typedef X Z;
struct Y : public Z
{
using Z::Z;
};
Run Code Online (Sandbox Code Playgroud)
问题是使用Z :: Z的行.它给了我一个错误C2039:'Z':不是'X'的成员
这段代码有效吗?或者这是一个错误?
在我下面的测试用例中,我很困惑为什么析构函数似乎没有被调用,即使我明确地调用它.我注意到只有在模板类型是指针时才会发生这种情况.
代码(内存泄漏,但我试图让最小的例子成为可能)
#include <iostream>
using namespace std;
class A {
public:
A() {}
~A() { cout << "A Destructor"; }
};
template<typename Type>
class TemplateTest {
protected:
Type* start;
Type* end;
Type* iter;
public:
void Generate(unsigned int count) {
start = new Type[count];
end = start + count;
iter = start;
}
void DestroyAll() {
for(; start < end; ++start) {
(*start).~Type();
}
}
void Push(Type val) {
*iter = val;
iter++;
}
};
int main() {
cout << "Non-pointer …Run Code Online (Sandbox Code Playgroud) 是否有一种优雅的方式在c ++ 11中执行条件 static_assert
例如:
template <class T>
class MyClass
{
COMPILE_TIME_IF( IsTypeBuiltin<T>::value)
static_assert(std::is_floating_point<T>::value, "must be floating pt");
};
Run Code Online (Sandbox Code Playgroud) 标题几乎传达了所有相关信息,但这里是一个最小的重复:
#include <atomic>
#include <cstdio>
#include <memory>
int main() {
auto ptr = std::make_shared<int>(0);
bool is_lockless = std::atomic_is_lock_free(&ptr);
printf("shared_ptr is lockless: %d\n", is_lockless);
}
Run Code Online (Sandbox Code Playgroud)
使用以下编译器选项进行编译会产生无锁shared_ptr实现:
g++ -std=c++11 -march=native main.cpp
Run Code Online (Sandbox Code Playgroud)
虽然这不是:
g++ -std=c++11 -march=native -pthread main.cpp
Run Code Online (Sandbox Code Playgroud)
GCCversion :( 5.3.0在Linux上,使用libstdc++),在多台机器上进行测试,这些机器应具有必要的原子指令才能使其工作.
有没有办法强制实现无锁实现(我需要无锁版本,无论性能如何)?
代码段说明了几个段落:
#include <boost/hana/fwd/eval_if.hpp>
#include <boost/hana/core/is_a.hpp>
#include <iostream>
#include <functional>
using namespace boost::hana;
template<class arg_t>
decltype(auto) f2(arg_t const& a)
{
constexpr bool b = is_a<std::reference_wrapper<std::string>,
arg_t>;
auto wrapper_case = [&a](auto _) -> std::string&
{ return _(a).get(); };
auto default_case = [&a]() -> arg_t const&
{ return a; };
return eval_if(b, wrapper_case, default_case);
}
int main()
{
int a = 3;
std::string str = "hi!";
auto str_ref = std::ref(str);
std::cout << f2(a) << ", " << f2(str) << ", " << f2(str_ref) …Run Code Online (Sandbox Code Playgroud) 我对unordered_map的默认构造函数有一些疑问.
这是代码:
unordered_map<int, bool> m;
cout <<boolalpha << m[0] << endl;
Run Code Online (Sandbox Code Playgroud)
输出是真还是假?
我知道元素是使用其默认构造函数构造的,但映射的值是true还是false?这是未定义的行为吗?
在vs2013中,输出是false.
实际上,我想删除数组中的重复元素.我想解决这个问题:
int a[] = {1, 2, 3, 1, 2, 3, 4};
unordered_map<int, bool> m;
int j = 0;
for (int i = 0; i < 7; ++i)
{
if (!m[a[i]])
{
a[j++] = a[i];
m[a[i]] = true;
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢,我真的很感激你的回答.
考虑下面运行C++ 11的代码.如果我理解正确的移动语义,则不应该调用复制构造函数.但它是.有人可以解释原因吗?
template<class D>
struct traced
{
public:
traced() = default;
traced(traced const&) { std::cout << typeid(D).name() << " copy ctor\n"; }
protected:
~traced() = default;
};
class A : public traced<A>{
public:
A(int x) : x_(x) {}
private:
int x_;
};
int main() {
// I thought the following two are equivalent. Apparently not.
aList.push_back(A(6)); // Prints out ".. copy ctor" ..
aList.emplace_back(6); // Does not print out " ... copy ctor"
}
Run Code Online (Sandbox Code Playgroud) 大脑疼了吗?
我正在研究tuple类型转换助手,并且遇到了问题.要么我重复代码,要么我包装template template.虽然我无法解决这个问题.
这是我需要使用的:
template<template<typename> class trans> struct no_index_transformer
{
template<size_t, typename transform_t> struct transformer
{
using t = typename trans<transform_t>::t;
};
};
Run Code Online (Sandbox Code Playgroud)
no_index_transformer采取template template的transformer是不具有size_t(指数)传递给它.内部transformer template是我需要通过的.我这样做的时候会出错,所以我不确定通过什么是正确的方法no_index_transformer<>::transformer.
我在以下行中收到错误:
template<template<typename> class transformer, typename tuple> using transform_tuple_t
= transform_tuple_index<no_index_transformer<transformer>::transformer, 0, tuple>::t;
Run Code Online (Sandbox Code Playgroud)
"模板模板参数的模板参数必须是类模板或类型别名模板."
这是变压器:
template<typename, typename> struct tuple_cat;
template<typename... types_one, typename... types_two> struct tuple_cat<std::tuple<types_one...>, std::tuple<types_two...>>
{
public:
using t = std::tuple<types_one..., types_two...>;
};
template<template<size_t, typename> class transformer, size_t index, …Run Code Online (Sandbox Code Playgroud) 我正在实现一个容器类(ObjectPool).它在连续的内存中维护一组模板对象.在构造时,它分配一块内存(相当于(模板对象的大小)*(池大小)).将新对象添加到池时,它使用'placement new'运算符在特定内存地址创建对象(并自动调用模板对象的构造函数).
我如何实现ObjectPool.add()方法,接受模板对象并将其添加到对象池,而不调用它的构造函数两次(例如在std :: vector.push_back()中实现的功能)?
为简单起见,在这种情况下,ObjectPool类只包含一个模板对象而不是数组.
class FooClass
{
public:
FooClass(int p_testValue) : m_testValue(p_testValue)
{
std::cout << "Calling constructor: " << m_testValue << std::endl;
}
int m_testValue;
};
template <class T_Object>
class ObjectPool
{
public:
ObjectPool()
{
// Allocate memory without initializing (i.e. without calling constructor)
m_singleObject = (T_Object*)malloc(sizeof(T_Object));
}
// I have tried different function arguments (rvalue reference here, amongs others)
inline void add(T_Object &&p_object)
{
// Allocate the template object
new (m_singleObject) T_Object(p_object);
}
T_Object *m_singleObject;
};
int …Run Code Online (Sandbox Code Playgroud) 双向迭代器意味着它是否支持reverse_iterator或类似迭代器,它将指向向量的结尾并递减迭代器,如下面的程序.
int main()
{
std::vector<int> myvector;
std::vector<int>::iterator it;
myvector.push_back(10);
myvector.push_back(101);
myvector.push_back(100);
for(it = --myvector.end();it!= myvector.begin();--it)
cout<<*it<<endl;
}
Run Code Online (Sandbox Code Playgroud) c++ ×11
c++11 ×7
templates ×3
c++14 ×2
atomic ×1
auto ×1
boost-hana ×1
cstring ×1
destructor ×1
lock-free ×1
rvalue ×1
shared-ptr ×1