小编Kap*_*pil的帖子

为什么这个构造函数没有给出不完整的类型错误?

我有两个文件test.h和main.cpp,如下所示:

test.h

#include <memory>

class TestImpl;

template <typename... T>
void createConnection(T&&... Args)
{
    // 1. Why is this working if the constructor is in cpp?
    std::unique_ptr<TestImpl> pimpl(new TestImpl(std::forward<T>(Args)...));
    std::cout << "Done..." << std::endl;

    // 2. Why is this not working if the constructor call has no issues?
    pimpl->sayHello();
}
Run Code Online (Sandbox Code Playgroud)

main.cpp中

#include <iostream>

#include "test.h"

class TestImpl
{
public:
    TestImpl(const std::string& first, const std::string& second)
        : _first(first)
        , _second(second)
    {
    }

    void sayHello()
    {
        std::cout << "Hello ... " << std::endl; …
Run Code Online (Sandbox Code Playgroud)

c++ types language-lawyer

13
推荐指数
1
解决办法
297
查看次数

为什么元类应该从类型继承?

我们可以使用函数作为元类,我的理解是它们不是从类型派生的,如下所示:

def test_meta(name, bases, atts):
    print("testmeta called for " + name)
    return type(name,bases,atts);

class Computer:
    __metaclass__ = test_meta
    def __init__(self,brand,model,price):
        self.brand = brand
        self.model = model
        self.price = price

#
#
ob = Computer('p1','core2duo',21985.25)
Run Code Online (Sandbox Code Playgroud)

然而,当我们编写元类时,它应该从类型继承,我无法理解这背后的原因:

class MyMeta:
    def __new__(meta, name, bases, dct):
        print ('-----------------------------------')
        print ("Allocating memory for class", name)
        print (meta)
        print (bases)
        print (dct)
        return type.__new__(meta, name, bases, dct)

    def __init__(cls, name, bases, dct):
        print ('-----------------------------------')
        print ("Initializing class", name)
        print (cls)
        print (bases)
        print (dct)
        type.__init__(cls,name, bases, …
Run Code Online (Sandbox Code Playgroud)

python metaclass

7
推荐指数
1
解决办法
914
查看次数

remove_reference如何禁用模板参数扣除?

根据这个链接,模板参数演绎不允许std::forward并且std::remove_reference正在帮助我们实现这一点.但是如何使用remove_reference防止模板扣除在这里发生?

template <class S>
S&& forward(typename std::remove_reference<S>::type& t) noexcept
{
    return static_cast<S&&>(t);
}
Run Code Online (Sandbox Code Playgroud)

c++ templates perfect-forwarding c++11

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

为什么仅在原语的情况下才在函数返回类型中忽略类型限定符?

在尝试从c ++项目中删除警告时,我无法理解为什么第一个函数返回int会发出警告"警告:在函数返回类型上忽略类型限定符"但是返回std :: string的第二个函数不会发出此警告?

   //first function
    const int getX()
    {
        int x =9;
        return x;
    }
   //second function
    const std::string getTemp()
    {
        std::string test = "Test..";
        return test;
    }
Run Code Online (Sandbox Code Playgroud)

c++

4
推荐指数
1
解决办法
1606
查看次数

C++20 三路比较运算符的等效和等于之间的区别?

在 c++20 中,引入了新的三路比较运算符 <=> (太空船运算符),它可以产生下面给出的四个值:

  1. std::strong_ordering::less
  2. std::strong_ordering::等效
  3. std::strong_ordering::等于
  4. std::strong_ordering::更大

然而,当我运行下面的代码片段时,似乎相等和等效都是一回事,所以我想知道它们之间有什么区别,以及在什么情况下我们应该更喜欢其中一个。

#include <iostream>
#include <compare>
int main()
{
    std::strong_ordering res = (2<=>2);
    if(res == std::strong_ordering::equivalent)
    {
        std::cout<<"Equivalent...."<<std::endl;
    }
    if(res == std::strong_ordering::equal)
    {
        std::cout<<"Equal"<<std::endl;
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出:

相等的....

平等的

c++ c++20

4
推荐指数
1
解决办法
851
查看次数

可以引用它指向的变量和不同的值吗?

我无法理解为什么在下面的代码片段引用及其相关变量给出不同的结果.

const int x = 10;
const int &ptr = x; 
int& y = const_cast<int&>(ptr);
y = 19;
std::cout << "x = " << x << " ptr=" << ptr << " y=" << y << std::endl;
Run Code Online (Sandbox Code Playgroud)

输出:

x=10 ptr=19 y=19
Run Code Online (Sandbox Code Playgroud)

按我的理解,引用只是变量别名,为什么ptr19x10?这是事做const还是const_cast

c++ casting const reference

3
推荐指数
2
解决办法
88
查看次数

constexpr string vs const string

我们正在将一些代码从c ++ 03迁移到c ++ 14,并且只要有一些性能提升,我们就要使用c ++ 14特性.现在我们的一个项目中我们正在根据列名解析csv列名在一个头文件中声明,如下所示:

const string ITEM_NAME = "Item Name";
const string ITEM_ID = "Item Id";
Run Code Online (Sandbox Code Playgroud)

有这样的一百个常量,所以我想知道的是,如果我将上面的代码更改为这样的代码,是否有任何显着的性能增益:

constexpr string ITEM_NAME = "Item Name";
constexpr string ITEM_ID = "Item Id";
Run Code Online (Sandbox Code Playgroud)

由于需要将它们存储在只读存储器中,因此我们可以在二进制文件中使用constexpr的数量有任何限制吗?

编译器是否也自动优化旧的c ++ 03代码并将const变量也放在只读内存中,这种努力是不值得的?

c++ constexpr c++14

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

自动如何推断指针类型?

在下面的代码中,我无法理解如何auto知道右侧的东西是指针:

int x = 100;
int *ptr = & x;
auto test = ptr;
std::cout<<*test<<std::endl;
Run Code Online (Sandbox Code Playgroud)

因为根据我的理解,指针包含的地址只不过unsigned int是如何将auto它推断为指针而不是unsigned int

c++ pointers auto

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

没有删除运算符的堆对象如何调用析构函数?

有人可以在这里解释一下,当未调用相应的删除时,如何通过 new 运算符为堆中创建的对象调用析构函数。此外,由于在下面的代码中我们通过常量引用捕获对象,而在析构函数中我们正在更改对象值(即设置 n=0),所以这怎么可能。

class A
{
    private:
        int n;
    public:
        A()
        {
            n=100;
            std::cout<<"In constructor..."<<std::endl;
        }
        ~A()
        {
            n=0;
            std::cout<<"In destructor..."<<std::endl;
        }
};
int main()
{
  try
  {
      throw *(new A());
  }
  catch(const A& obj)
  {
      std::cout<<"Caught...."<<std::endl;
  }

 return 0;
}
Run Code Online (Sandbox Code Playgroud)

程序的输出(在http://cpp.sh/3jm4x 上运行):

在构造函数中...
抓住....
在析构函数...

c++ exception new-operator

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

为什么unique_ptr不允许const_cast?

我正在尝试 const_cast unique_ptr 它给了我错误:

  const std::unique_ptr<int> myptr;
  std::unique_ptr<int> myptr1 = std::move(const_cast<std::unique_ptr<int> >(myptr));
Run Code Online (Sandbox Code Playgroud)

所以我想了解为什么 const_cast 不能与 unique_ptr 一起使用,如果它可以与普通指针一起使用?

c++ casting constants unique-ptr c++11

2
推荐指数
1
解决办法
2351
查看次数