- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一个 const 引用的元组 std::tuple<const Matrix&, ...>
我从中构建一个值元组 std::tuple<Matrix, ...>
.对于大于 1 的任何大小的元组,这都可以正常工作:(在线示例:https://godbolt.org/g/24E8tU)
#include <tuple>
struct Matrix {
Matrix() = default;
Matrix(Matrix const&) = default;
template <typename T>
explicit Matrix(T const&) {
// in reality, this comes from Eigen, and there is real work
// being done here. this is just to demonstrate that the code
// below fails
static_assert(std::is_same<T, int>::value, "!");
}
};
void works() {
Matrix m1, m2;
std::tuple<const Matrix &, const Matrix &> tuple_of_ref{m1, m2};
std::tuple<Matrix, Matrix> t{tuple_of_ref};
}
但是,对于大小为 1 的元组,此代码无法编译:
void fails() {
Matrix m;
std::tuple<const Matrix &> tuple_of_ref{m};
// Tries and fails to instantiate Matrix(std::tuple<const Matrix &>)
std::tuple<Matrix> t{tuple_of_ref};
}
注意 Matrix
类有一个模板化的构造函数,它接受 std::tuple
.
template<typename T>
explicit Matrix(const T& x)
我不想使用这个构造函数,而且我无法更改它,因为它是第三方代码。
我想我的works()
示例正确调用在 cppreference 上列为 #4 的构造函数:
template< class... UTypes >
tuple( const tuple<UTypes...>& other );4) Converting copy-constructor. For all i in
sizeof...(UTypes)
, initializes ith element of the tuple withstd::get<i>(other)
.
fails()
示例尝试使用 this constructor ,大概是 #3,我不想要:
template< class... UTypes >
explicit tuple( UTypes&&... args );3) Converting constructor. Initializes each element of the tuple with the corresponding value in
std::forward<Utypes>(args)
.
我怎样才能确定tuple
的构造函数 #4 用于这两种情况?我的真实用例是在可变参数模板中,所以我事先不知道元组的大小。
最佳答案
是的,所以...这就是问题所在:
template<typename T>
explicit Matrix(const T& x)
那是一个非常不友好的构造函数——因为它在撒谎。 Matrix
实际上不能从任何东西构造,只是一些特定的东西——但没有办法从外部检测这些东西是什么。
当考虑怎样构建一个tuple<Matrix>
来自tuple<Matrix const&>
, 我们有 lots of choices , 但实际上只有两个可行:
// #2, with Types... = {Matrix}
tuple(Matrix const&);
// #3, with UTypes = {tuple<Matrix const&>&}
tuple(tuple<Matrix const&>&);
双方最终都试图构建一个 Matrix
来自tuple<Matrix const&>
,这是行不通的,你被卡住了。
现在,您可能认为 #4 是您的救星 - 生成一个构造函数:
tuple(tuple<Matrix const&> const& );
并构建其基础Matrix
来自 tuple
论证的基础 Matrix
.也就是说,使用转换复制构造函数。似乎问题在于此构造函数有效,但无论出于何种原因(即它采用较少的 cv 限定引用),#3 都是首选,解决方案是尝试摆弄参数,以便首选 #4(即通过在参数上使用 as_const()
。
但该构造函数并非不受欢迎...实际上它在这里不可行 因为对 that constructor is 的限制(来自 LWG 2549 ):
either
sizeof...(Types) != 1
, or (whenTypes...
expands toT
andUTypes...
expands toU
)is_convertible_v<const tuple<U>&, T>
,is_constructible_v<T, const tuple<U>&>
, andis_same_v<T, U>
are allfalse
.
但我们确实有 sizeof..(Types) == 1
并且这些事情并非都是错误的(特别是,第二个是正确的 - 这是你所有问题的根源),所以#4根本不是候选人并且没有真正巧妙的技巧可以制作它一个。
那么,如何解决呢? 到目前为止要做的最好的事情就是修复Matrix
.我知道这不太可能,但不得不说。
你可以包装 Matrix
在实际上向其构造函数添加约束以避免此问题的东西中。由于您已经将其复制到 tuple
中,这让您有机会做一些简单的事情,例如:
template <typename T>
struct only_copyable {
only_copyable(only_copyable const& ) = default;
only_copyable(T const& t) : t(t) { }
template <typename U> only_copyable(U const& ) = delete;
T t;
};
但您可能想要比这更现实的东西。您的类型也可以继承自 Matrix
为了理智起见,只是摆弄它的构造函数。
或者,在专门处理 tuple
时s 的大小为 1,您可以避免使用元组构造函数而只使用默认构造/分配。或者显式调用 get<0>
或类似的东西。
或者,您可以避免 tuple
s 大小 1 完全。这是一件奇怪的具体事情,但也许这就足够了(或者你可以包装 tuple
使得 my_tuple<A, B, C...>
是 tuple<A,B,C...>
但 my_tuple<A>
实际上是 tuple<A, monostate>
)。
但真的...修复 Matrix
构造函数似乎非常值得。
关于c++ - 使用单参数模板化构造函数从引用元组构造值元组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49988289/
#include using namespace std; class C{ private: int value; public: C(){ value = 0;
这个问题已经有答案了: What is the difference between char a[] = ?string?; and char *p = ?string?;? (8 个回答) 已关闭
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 7 年前。 此帖子已于 8 个月
除了调试之外,是否有任何针对 c、c++ 或 c# 的测试工具,其工作原理类似于将独立函数复制粘贴到某个文本框,然后在其他文本框中输入参数? 最佳答案 也许您会考虑单元测试。我推荐你谷歌测试和谷歌模拟
我想在第二台显示器中移动一个窗口 (HWND)。问题是我尝试了很多方法,例如将分辨率加倍或输入负值,但它永远无法将窗口放在我的第二台显示器上。 关于如何在 C/C++/c# 中执行此操作的任何线索 最
我正在寻找 C/C++/C## 中不同类型 DES 的现有实现。我的运行平台是Windows XP/Vista/7。 我正在尝试编写一个 C# 程序,它将使用 DES 算法进行加密和解密。我需要一些实
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
有没有办法强制将另一个 窗口置于顶部? 不是应用程序的窗口,而是另一个已经在系统上运行的窗口。 (Windows, C/C++/C#) 最佳答案 SetWindowPos(that_window_ha
假设您可以在 C/C++ 或 Csharp 之间做出选择,并且您打算在 Windows 和 Linux 服务器上运行同一服务器的多个实例,那么构建套接字服务器应用程序的最明智选择是什么? 最佳答案 如
你们能告诉我它们之间的区别吗? 顺便问一下,有什么叫C++库或C库的吗? 最佳答案 C++ 标准库 和 C 标准库 是 C++ 和 C 标准定义的库,提供给 C++ 和 C 程序使用。那是那些词的共同
下面的测试代码,我将输出信息放在注释中。我使用的是 gcc 4.8.5 和 Centos 7.2。 #include #include class C { public:
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我的客户将使用名为 annoucement 的结构/类与客户通信。我想我会用 C++ 编写服务器。会有很多不同的类继承annoucement。我的问题是通过网络将这些类发送给客户端 我想也许我应该使用
我在 C# 中有以下函数: public Matrix ConcatDescriptors(IList> descriptors) { int cols = descriptors[0].Co
我有一个项目要编写一个函数来对某些数据执行某些操作。我可以用 C/C++ 编写代码,但我不想与雇主共享该函数的代码。相反,我只想让他有权在他自己的代码中调用该函数。是否可以?我想到了这两种方法 - 在
我使用的是编写糟糕的第 3 方 (C/C++) Api。我从托管代码(C++/CLI)中使用它。有时会出现“访问冲突错误”。这使整个应用程序崩溃。我知道我无法处理这些错误[如果指针访问非法内存位置等,
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 7 年前。
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,因为
我有一些 C 代码,将使用 P/Invoke 从 C# 调用。我正在尝试为这个 C 函数定义一个 C# 等效项。 SomeData* DoSomething(); struct SomeData {
这个问题已经有答案了: Why are these constructs using pre and post-increment undefined behavior? (14 个回答) 已关闭 6
我是一名优秀的程序员,十分优秀!