sizeof和alignof有什么区别?
#include <iostream>
#define SIZEOF_ALIGNOF(T) std::cout<< sizeof(T) << '/' << alignof(T) << std::endl
int main(int, char**)
{
SIZEOF_ALIGNOF(unsigned char);
SIZEOF_ALIGNOF(char);
SIZEOF_ALIGNOF(unsigned short int);
SIZEOF_ALIGNOF(short int);
SIZEOF_ALIGNOF(unsigned int);
SIZEOF_ALIGNOF(int);
SIZEOF_ALIGNOF(float);
SIZEOF_ALIGNOF(unsigned long int);
SIZEOF_ALIGNOF(long int);
SIZEOF_ALIGNOF(unsigned long long int);
SIZEOF_ALIGNOF(long long int);
SIZEOF_ALIGNOF(double);
}
Run Code Online (Sandbox Code Playgroud)
将输出
1/1 1/1 2/2 2/2 4/4 4/4 4/4 4/4 4/4 8/8 8/8 8/8
我想我不知道对齐是什么......?
#include <iostream>
#include <type_traits>
double f(int i)
{
return i+0.1;
}
struct F
{
public:
double operator ()(int i) { return i+0.1; }
};
int
main(int, char**)
{
std::result_of<F(int)>::type x; // ok
// std::result_of<f(int)>::type x; // error: template argument 1 is invalid
x = 0.1;
std::cerr << x << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
请解释为什么std::result_of<f(int)>::type x;无效......
cppreference说"(std::result_of)在编译类型中扣除函数调用表达式的返回类型."
有什么问题?
你能解释一下"完美转发"的工作原理吗?
我读到vector的emplace_back不需要复制或移动对象,因为它的参数是作为可变参数模板实现的.
std::vector<T>::emplace_back(_Args&&... __args)
Run Code Online (Sandbox Code Playgroud)
你能更详细地描述一下吗?为什么不复制或移动?
请解释enum以下实施电源模板时使用的内容.
template<int B, int N>
struct Pow {
// recursive call and recombination.
enum{ value = B*Pow<B, N-1>::value };
};
template< int B >
struct Pow<B, 0> {
// ''N == 0'' condition of termination.
enum{ value = 1 };
};
int quartic_of_three = Pow<3, 4>::value;
Run Code Online (Sandbox Code Playgroud)
我在维基百科上找到了它.有没有之间的差异int,并enum在这种情况下?
#include <iostream>
class Class
{
public:
Class() { std::cerr << "ctor" << std::endl; }
~Class() { std::cerr <<"dtor" << std::endl; }
Class(Class&) { std::cerr << "copy ctor" << std::endl; }
Class & operator=(const Class &)
{
std::cerr << "copy operator=" << std::endl;
return *this;
}
Class(Class&&) { std::cerr << "move ctor" << std::endl;}
Class & operator=(Class &&)
{
std::cerr << "move operator="<< std::endl;
return *this;
}
};
int main(int, char**)
{
Class object;
Class && rvr = Class();
object = …Run Code Online (Sandbox Code Playgroud)