小编use*_*506的帖子

sizeof和alignof有什么区别?

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

我想我不知道对齐是什么......?

c++ sizeof c++11 alignof

45
推荐指数
6
解决办法
1万
查看次数

std :: result_of简单函数

#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)在编译类型中扣除函数调用表达式的返回类型."

有什么问题?

c++ std result-of c++11

19
推荐指数
1
解决办法
6481
查看次数

vector的emplace_back

你能解释一下"完美转发"的工作原理吗?

我读到vector的emplace_back不需要复制或移动对象,因为它的参数是作为可变参数模板实现的.

std::vector<T>::emplace_back(_Args&&... __args)
Run Code Online (Sandbox Code Playgroud)

你能更详细地描述一下吗?为什么不复制或移动?

c++ stl c++11

18
推荐指数
1
解决办法
2464
查看次数

来自wiki的pow(power)模板实现

可能重复:
模板元编程 - 使用Enum Hack和静态Const之间的区别

请解释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在这种情况下?

c++ enums templates

5
推荐指数
1
解决办法
845
查看次数

C++ 11:当调用move ctor/operator =时?

#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)

c++ c++11

3
推荐指数
1
解决办法
2020
查看次数

标签 统计

c++ ×5

c++11 ×4

alignof ×1

enums ×1

result-of ×1

sizeof ×1

std ×1

stl ×1

templates ×1