以下程序编译正常.
#include <iostream>
#include <bitset>
void foo(std::bitset<10> n)
{
std::cout << n.size() << "\n";
}
int main()
{
std::bitset<10> n;
foo(n);
}
Run Code Online (Sandbox Code Playgroud)
$ g++ -std=c++11 -Wall -Wextra -pedantic foo.cpp
$ ./a.out
10
Run Code Online (Sandbox Code Playgroud)
如何修改foo()函数以使其可以接受bitset任何大小?
鉴于以下代码
#include <iostream>
using namespace std;
template <typename Type>
struct Something {
Something() {
cout << "Something()" << endl;
}
template <typename SomethingType>
Something(SomethingType&&) {
cout << "Something(SomethingType&&)" << endl;
}
};
int main() {
Something<int> something_else{Something<int>{}};
auto something = Something<int>{};
Something<int>{something};
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我得到以下输出
Something()
Something()
Something(SomethingType&&)
Run Code Online (Sandbox Code Playgroud)
为什么复制构造函数被解析为模板化转发引用构造函数而不是移动构造函数?我猜这是因为移动构造函数是隐式定义的,而不是复制构造函数。但是在阅读了堆栈溢出中未隐式定义复制构造函数的情况后,我仍然感到困惑。
所以这是一个非常基本的问题和超级微不足道的但我只是通过c ++中的编程原则和实践,我的程序用于读取字符串和int的行为与Bjarne Stroustrup编写的书不同,所以如果他感到惊讶犯了一个错误.无论如何这里是代码:
#include "..\std_lib_facilities.h"
int main()
{
cout << "Please enter your first name and age\n";
string first_name = "???"; // string variable
// ("???” means “don’t know the name”)
int age = -1; // integer variable (1 means “don’t know the age”)
cin >> first_name >> age; // read a string followed by an integer
cout << "Hello, " << first_name << " (age " << age << ")\n";
}
Run Code Online (Sandbox Code Playgroud)
当我在提示符输入"22 Carlos"时它输出"Hello,22(0岁)",基本上使我的错误检查的初始化值无用.这是c ++或其他东西的新功能,这就是为什么这本书出错了?
编辑1:BTW我在Windows 7上使用GCC for cygwin,并且-std = …
我希望一个类具有 , 的两种不同实现push,并根据布尔模板参数进行选择。我尝试使用本答案中描述的 SFINAE 原则,如下所示:
template<class T, bool foo=true>
class Bar {
template <>
typename std::enable_if<foo>::type
push(const T& value) { /* one implementation */}
template <>
typename std::enable_if<!foo>::type
push(const T& value) { /* another implementation */ }
}
Run Code Online (Sandbox Code Playgroud)
但是,我push在 gcc 下收到“无法专门化类范围内的函数”的错误,我不明白为什么。尽管我的代码与链接答案中的代码不完全一样,但它似乎非常相似,我无法发现关键区别。
我还尝试使用与此答案中建议的语法类似的语法,但它也不起作用(错误是“不能重新声明类成员”):
template <bool enable=foo>
typename std::enable_if<enable>::type
push(const T& value) { /* one implementation */}
template <bool enable=!foo>
typename std::enable_if<enable>::type
push(const T& value) { /* another implementation */ }
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
我看到一些代码片段如下:
std::unique_ptr<uint8_t> mCache;
mCache.reset(new uint8_t[size]);
Run Code Online (Sandbox Code Playgroud)
有人告诉我这个代码有一些问题.谁能给我一些细节?
第一次尝试,一切正常:
class Base {
public:
Base() {std::cout << "default ctor!\n"; }
};
...
Base b{};
Base b_one = {};
Run Code Online (Sandbox Code Playgroud)
另一种实现方式(添加explicit):
class Base {
public:
explicit Base() {std::cout << "default ctor!\n"; }
};
...
Base b{};
Base b_one = {}; // error! Why?
Run Code Online (Sandbox Code Playgroud)
我已经读过cppreference,在这两种情况下都会使用默认初始化而不会出现diffences.
从列表初始化:
否则,如果braced-init-list为空且T是具有默认构造函数的类类型,则执行值初始化.
从值初始化:
如果T是没有默认构造函数的类类型,或者是用户提供或删除的默认构造函数,则该对象是默认初始化的;
我在这里已经看到了这个问题的许多变体,但是我仍然觉得我的具体情况有所不同。
我的目标是包装一个如下所示的C API:
TF_Buffer* buf = TF_AllocateBuffer();
// ...
TF_DeleteBuffer(buf);
Run Code Online (Sandbox Code Playgroud)
由于我有许多这样的对象,因此我很想创建一个名为的通用类型handle,该类型可以容纳给定的指针,并在销毁时调用适当的释放器。我想象的用例是
class buffer : public handle<TF_Buffer, TF_DeleteBuffer> {
public:
buffer(TF_Buffer* b): handle(b) {}
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,由于这TF_DeleteBuffer是一个简单的函数(类型void TF_DeleteBuffer(TF_Buffer*)),我无法使其正常工作。我确实设法通过为函数创建函数对象来解决此问题,因此以下内容确实有效
template<typename Obj, typename Deleter>
class handle {
public:
Obj* obj;
handle(Obj* o): obj(o) {};
~handle() { if (obj) Deleter()(obj); }
};
struct buffer_deleter {
void operator()(TF_Buffer* b) { TF_DeleteBuffer(b); }
};
class buffer : public handle<TF_Buffer, buffer_deleter> {
public:
buffer(TF_Buffer* b): handle(b) {}
}
Run Code Online (Sandbox Code Playgroud)
但是buffer_deleter仅为此目的而定义类感觉很脏。我想像这样的东西应该工作(带或不带std::function …
我有一个类型为type的非类型模板参数的函数int,如下所示:
template <int N>
int foo() { /*...*/ }
Run Code Online (Sandbox Code Playgroud)
我想对该函数N从0到32的所有值进行单元测试。我有一个函数int expected(int n)采用相同的N值并返回期望值。实际上,我想要:
if (foo<0>() != expected(0)) { /* fail... */ }
if (foo<1>() != expected(1)) { /* fail... */ }
if (foo<2>() != expected(2)) { /* fail... */ }
// 30 more lines
Run Code Online (Sandbox Code Playgroud)
我不想手工写出所有33个测试用例,而且由于N编译时的原因,我不能轻易使用运行时循环。
如何BOOST_PP_REPEAT在C ++ 11中让编译器以简单的方式为我生成测试用例,而无需-style技巧或代码生成?
std::distance在 上给我一个圆形距离std::list,而不是相对距离。为什么?
#include <list>
#include <iostream>
#include <iterator>
using namespace std;
int main(){
list<int> derp = {1,2,3,4};
auto begin = derp.begin();
auto end = derp.end();
end--;
cout << distance(end, begin) << endl;
cout << distance(begin, end) << endl;
}
Run Code Online (Sandbox Code Playgroud)
当我运行它时,会发生以下输出:
2
3
Run Code Online (Sandbox Code Playgroud)
我期望以下内容:
-3
3
Run Code Online (Sandbox Code Playgroud)
为什么会这样?
以下代码始终有效还是依赖于编译器/平台?显然,我可以edges使用值构造函数进行初始化,但是我很好奇operator=在edges初始化为大小 0时复制赋值是否在此处工作,然后设置为等于带括号的 r 值。
它适用于我的 macbook。
std::vector<std::vector<int>> edges;
edges = {{1,2,3},{4},{5,6}};
Run Code Online (Sandbox Code Playgroud)