小编Wut*_*utz的帖子

移动唯一指针 - cppreference 上的未定义行为?

我对此有一个后续问题:Move unique_ptr: Reset the source vs. destroy the old object

为了快速总结原始问题,cppreference上有以下示例代码:

struct List
{
    struct Node
    {
        int data;
        std::unique_ptr<Node> next;
    };
 
    std::unique_ptr<Node> head;
 
    ~List()
    {
        // destroy list nodes sequentially in a loop, the default destructor
        // would have invoked its `next`'s destructor recursively, which would
        // cause stack overflow for sufficiently large lists.
        while (head)
            head = std::move(head->next);
    }
 
    ...
};
Run Code Online (Sandbox Code Playgroud)

答案告诉我,析构函数中的循环就所包含的原始指针而言是明确定义的,因为unique_ptr::operator=(unique_ptr&& other)被定义为 call ,这保证了在删除所持有的 raw_pointer 之前reset(other.release())提取原始指针。otherthis

我相信 cppreference …

c++ unique-ptr language-lawyer move-semantics

23
推荐指数
1
解决办法
969
查看次数

在C#中使用C++组件对象模型

我正在尝试用C++构建一个COM库,使用C#项目进行测试.某些方法需要将字符串返回给调用者.从C#调用这些方法后,我得到:"访问违规读取位置..."

这是我的testproject的C++代码(除了VS 2010 ATL生成的所有内容)

//COMTest.idl
[id(1)] HRESULT Test([out,retval] BSTR* ret);

//Program2.h
STDMETHOD(Test)(BSTR* ret);

//Program2.cpp
STDMETHODIMP CProgram2::Test(BSTR* ret)
{
   BSTR tmp = (BSTR)CoTaskMemAlloc(sizeof(wchar_t) * 2);
   tmp[0] = L'H';
   tmp[1] = L'\0';

   *ret = (BSTR)tmp;
   return S_OK;
}
Run Code Online (Sandbox Code Playgroud)


在C#中,我刚从COM-Tab引用了DLL,关闭了"嵌入互操作类型",因为它导致了错误,并运行了这个:

static void Main(string[] args)
{
   COMTestLib.Program2Class instance = new COMTestLib.Program2Class();
   string tmp = instance.Test(); //Where the error occurs

   Console.WriteLine(tmp); //This is not reached

   Console.Read();
}
Run Code Online (Sandbox Code Playgroud)

离开测试方法后发生错误.我在C#项目中调试了C++代码,并将值放在正确的位置.如果我尝试返回0(在C#中为null),即使我仍然像示例中那样分配内存,我也不会收到错误.

我无法理解访问冲突所抱怨的地址.它既不是我分配的地址,也不是该方法中使用的任何其他地址.对我来说似乎很奇怪的是CoTaskMemAlloc-Function总是返回第一个字节设置为零(0x00XXXXXX)的地址,但这可能只是一个COM事物.


我没有想法,我无法在任何地方找到关于此的更多信息(除了基本的COM教程).有人可以帮忙吗?

c# c++ com

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

基于具有相关参数的概念的重载解决方案

我正在尝试使用概念来重载其参数取决于模板参数的模板化函数。不幸的是,这在 gcc 上失败了,报告了一个不明确的过载。

在我的项目中,我的代码在 gcc 9.3 中编译,但在 gcc 12.2 中失败。但在尝试最小化代码时,我最终得到了这个,这在两个 gcc 版本上都失败了,但在 clang 15.0.0 中有效:

#include <type_traits>
#include <iostream>

struct A
{
    using value_type = int;
};
struct B
{
    using value_type = int;
};

template<typename Candidate>
concept something = requires {
    typename Candidate::value_type;
};
template<typename Candidate>
concept something_specific = something<Candidate> && std::is_same_v<Candidate, A>;

template<something T>
void foo()
{
    std::cout << "Something" << std::endl;
}
template<something_specific T>
void foo()
{
    std::cout << "Something specific" << std::endl;
} …
Run Code Online (Sandbox Code Playgroud)

c++ language-lawyer overload-resolution c++-concepts c++20

5
推荐指数
0
解决办法
142
查看次数

cppreference 上的partial_ordering 措辞错误

在 CPP Reference 对std::partial_ordering的描述中,它说:

\n
\n

partial_ordering尝试将 a与整数文字以外的任何内容进行比较的程序的行为\xe2\x80\x8b0\xe2\x80\x8b是未定义的。

\n
\n

但随后它记录了一个比较 的两个实例的相等运算符partial_ordering。在我看来这是一个矛盾。

\n

我理解他们想表达的意思:如果您要与整数进行比较,它必须是文字0,否则它是未定义的行为。但现在的措辞方式看起来像是在比较两个partial_ordering实例相互比较似乎也是未定义的行为。

\n

我是不是误读了什么?“尝试比较”不包含平等比较吗?

\n

c++ c++20

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