如果我想将已知或未知大小的数组作为函数参数传递,我对使用哪种语法感到困惑.
假设我有这些变体用于此目的:
void func1(char* str) {
//print str
}
void func2(char str[]) {
//print str
}
void func3(char str[10]) {
//print str
}
Run Code Online (Sandbox Code Playgroud)
使用这些中的每一个有什么利弊?
将浮点常量声明为static constexpr变量和函数(如下例所示)之间是否存在差异,还是仅仅是样式问题?
class MY_PI
{
public:
static constexpr float MY_PI_VAR = 3.14f;
static constexpr float MY_PI_FUN() { return 3.14f; }
}
Run Code Online (Sandbox Code Playgroud) 我正在研究如何在C++中将成员的内存偏移量转换为类,并在维基百科上看到了这一点:
在C++代码中,您不能使用offsetof来访问非Plain Data Data Structures的结构或类的成员.
我尝试了它似乎工作正常.
class Foo
{
private:
int z;
int func() {cout << "this is just filler" << endl; return 0;}
public:
int x;
int y;
Foo* f;
bool returnTrue() { return false; }
};
int main()
{
cout << offsetof(Foo, x) << " " << offsetof(Foo, y) << " " << offsetof(Foo, f);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我收到了一些警告,但它已经编译,运行时它给出了合理的输出:
Laptop:test alex$ ./test
4 8 12
Run Code Online (Sandbox Code Playgroud)
我想我要么误解POD数据结构是什么,要么我错过了其他一些难题.我不知道问题是什么.
我是否有一种标准方法可以在编译时在c ++ 11中的无符号索引上选择类型?
例如,类似于:
using type_0 = static_switch<0,T,U>; // yields type T
using type_1 = static_switch<1,T,U>; // yields type U
Run Code Online (Sandbox Code Playgroud)
如果有一个variadic-template版本,那将非常有用.
我可以使用模板别名作为模板模板参数吗?
template <template <typename...> class> struct foo {};
template <typename T> using simple_ptr = std::unique_ptr<T>;
foo<std::unique_ptr> a; // this doesn't work, std::unique_ptr has two parameters
foo<simple_ptr> b; // does this work?
Run Code Online (Sandbox Code Playgroud) 当我运行此代码时:
struct X {
int a;
};
struct Y : public X {};
X x = {0};
Y Y = {0};
Run Code Online (Sandbox Code Playgroud)
我明白了:
error: could not convert ‘{0}’ from ‘<brace-enclosed initializer list>’ to ‘Y’
Run Code Online (Sandbox Code Playgroud)
为什么大括号初始化适用于基类而不适用于派生类?
C++ 11引入了alignas说明符来指定变量的对齐方式,以及alignof运算符来查询类型的默认对齐方式.但是,我没有看到任何方法来获得特定变量的对齐方式.让我们采取以下简单的例子:
alignas(16) float* array;
Run Code Online (Sandbox Code Playgroud)
以下是我们可以做的事情:
alignof(float*) 返回8,这显然不是我们想要的.alignof(array)返回16,这正是我们想要的,但这是一个编译器扩展; alignof由标准指定不能用于特定变量.alignof(decltype(array)) 返回8,这是非常期待但不是我们想要的.std::alignment_of是实施的alignof,所以它没有多大帮助.我想要一种机制来确认特定变量array是否在16字节边界上对齐.标准中是否有任何内容可以执行此类查询?
我最近询问了有关Code Review 的问题,以检查名为QuickMergeSort的排序算法.我不会详细介绍,但在某些时候算法执行内部mergesort:它不是使用额外的内存来存储要合并的数据,而是交换元素以与原始序列的另一部分中的元素合并,这不是否则就合并而言.这是我关注的算法的一部分:执行合并的函数:
template<
typename InputIterator1,
typename InputIterator2,
typename OutputIterator,
typename Compare = std::less<>
>
auto half_inplace_merge(InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,
OutputIterator result, Compare compare={})
-> void
{
for (; first1 != last1; ++result) {
if (first2 == last2) {
std::swap_ranges(first1, last1, result);
return;
}
if (compare(*first2, *first1)) {
std::iter_swap(result, first2);
++first2;
} else {
std::iter_swap(result, first1);
++first1;
}
}
// first2 through last2 are already in the right spot
}
Run Code Online (Sandbox Code Playgroud)
该函数改编自libc …
我已经使用了SFINAE习惯用了很多次,我习惯于把std::enable_if<>模板参数放在模板参数而不是返回类型中.但是,我遇到了一些不起作用的琐碎案例,我不知道为什么.首先,这是我的主要内容:
int main()
{
foo(5);
foo(3.4);
}
Run Code Online (Sandbox Code Playgroud)
这是一个foo触发错误的实现:
template<typename T,
typename = typename std::enable_if<std::is_integral<T>::value>::type>
auto foo(T)
-> void
{
std::cout << "I'm an integer!\n";
}
template<typename T,
typename = typename std::enable_if<std::is_floating_point<T>::value>::type>
auto foo(T)
-> void
{
std::cout << "I'm a floating point number!\n";
}
Run Code Online (Sandbox Code Playgroud)
这是一个可以正常工作的等效代码:
template<typename T>
auto foo(T)
-> typename std::enable_if<std::is_integral<T>::value>::type
{
std::cout << "I'm an integrer!\n";
}
template<typename T>
auto foo(T)
-> typename std::enable_if<std::is_floating_point<T>::value>::type
{
std::cout << "I'm a floating point number!\n";
}
Run Code Online (Sandbox Code Playgroud)
我的问题是:为什么第一次执行 …
为什么st_ :: initializer_list <_E> :: size在static_assert中是不允许的,即使它在我的libstdc ++(v.4.6)中被声明为constexpr?
例如,以下代码:
template<class T, int Length>
class Point
{
public:
Point(std::initializer_list<T> init)
{
static_assert(init.size() == Length, "Wrong number of dimensions");
}
};
int main()
{
Point<int, 3> q({1,2,3});
return 0;
}
Run Code Online (Sandbox Code Playgroud)
给出以下错误:
test.C: In constructor ‘Point<T, Length>::Point(std::initializer_list<_Tp>) [with T = int, int Length = 3]’:
test.C:60:26: instantiated from here
test.C:54:7: error: non-constant condition for static assertion
test.C:54:73: in constexpr expansion of ‘init.std::initializer_list<_E>::size [with _E = int, std::initializer_list<_E>::size_type = long unsigned int]()’
test.C:54:7: error: ‘init’ …Run Code Online (Sandbox Code Playgroud) c++ ×10
c++11 ×7
templates ×4
constexpr ×2
arrays ×1
compile-time ×1
offsetof ×1
performance ×1
pointers ×1
sfinae ×1
sorting ×1
struct ×1
type-traits ×1