- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
抱歉,标题太笼统了,但我无法聚焦问题。
我有一个模板化的类方法,它接受一个参数包并提供一个新类型作为返回,以隐藏实现的细节。更具体地说,该类处理 SQLite 查询,该方法调用 sqlite3_prepare()
以在执行查询之前准备语句。
class Table {
...
template <typename ...Ts>
class PreparedStatement { ... };
template <typename ...Ts>
PreparedStatement<Ts...> prepare(std::tuple<Ts...> tuple) {
// do something
return PreparedStatement<Ts...> ( ... );
}
这适用于“普通”类型,但当参数声明为 const
时会出现问题:
const Field<int> fld = createField<int>("name");
...
PreparedStatement<decltype(fld)> s = prepare(make_tuple(fld));
错误如下:
no match for 'operator =' (operand types are PreparedStatenent<const Field<int>> and PreparedStatement<Field<int>>
我怀疑问题出在我的函数声明中,有没有办法解决这个问题并使函数更“优雅”?
注意:我知道我可以通过手动声明 s
变量来解决这个问题,但我怀疑该方法是如何实现的。
正如许多人所问,这里有一个例子:
#include <tuple>
template <typename T>
struct Field {
};
class Table {
public:
template <typename ...Ts>
class PreparedStatement {
public:
PreparedStatement() {};
};
template <typename ...Ts>
PreparedStatement<Ts...> prepare(std::tuple<Ts...> tuple) {
// do something
return PreparedStatement<Ts...> ( );
}
};
int main()
{
Field<int> fld;
Table t;
Table::PreparedStatement<decltype(fld)> p;
p = t.prepare(std::make_tuple(fld));
// here comes the problem
const Field<int> f2;
Table::PreparedStatement<decltype(f2)> p2;
p2 = t.prepare(std::make_tuple(f2));
return 0;
}
这是编译器的输出
main.cpp: In function 'int main()': main.cpp:35:39: error: no match for 'operator=' (operand types are 'Table::PreparedStatement >' and 'Table::PreparedStatement >') p2 = t.prepare(std::make_tuple(f2)); ^ main.cpp:10:10: note: candidate: constexpr Table::PreparedStatement >& Table::PreparedStatement >::operator=(const Table::PreparedStatement >&) class PreparedStatement { ^~~~~~~~~~~~~~~~~ main.cpp:10:10: note: no known conversion for argument 1 from 'Table::PreparedStatement >' to 'const Table::PreparedStatement >&' main.cpp:10:10: note: candidate: constexpr Table::PreparedStatement >& Table::PreparedStatement
::operator=(Table::PreparedStatement >&&) main.cpp:10:10: note: no known conversion for argument 1 from 'Table::PreparedStatement >' to 'Table::PreparedStatement >&&'
更新正如许多人指出的那样,我可以使用 auto
来推断类型,但在某些情况下 auto
实际上无法使用。一个是,例如,如果我需要在类上下文中声明语句。所以假设 auto
由于某种原因被禁止。没有其他可用的解决方案吗?请参阅上面的更新代码。
最佳答案
cppreference.com make_tuple告诉我们:
template< class... Types >
tuple<VTypes...> make_tuple( Types&&... args );For each
Ti
inTypes...
, the corresponding typeVi
inVtypes...
isstd::decay<Ti>::type
unless application ofstd::decay
results instd::reference_wrapper<X>
for some typeX
, in which case the deduced type isX&
.
同时 std::decay
,除其他事项外,删除 cv 限定符。所以你的类型将是 no PreparedStatement<const Field<int>>
,但是PreparedStatement<Field<int>>
.
您可以使用 auto
,正如 manni66 所建议的那样,以避免此类问题。
auto s = prepare(make_tuple(fld));
关于C++ 模板 : correct way to return a new type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48621516/
我第一次决定切换到 InnoDB 并尝试使用外键和其他 InnoDB 功能。 创建关系时,我应该只在一张表上声明它们吗?还是两个表? 例如,对于以下每种情况,您将在何处以及如何声明关系? 1 用户有很
老方法 当我以前在需要内容被搜索引擎索引的项目中异步加载页面时,我使用了一种非常简单的技术,那就是 Page $('#example').click(function(){
我目前正在为自己创建自己的自定义应用程序来编译 Java 文件。我的应用程序可以完美地编译 Java 文件,但现在我想开始为 Java 文件添加某种类型的测试(例如,我将一些变量传递给许多不同的文件
我需要建立从我的 iPhone 应用程序到客户服务器的 HTTPS 双向 SSL 连接。但是我没有看到任何安全的方式来将客户端证书传递给应用程序(这是一个电子银行应用程序,所以安全性确实是一个问题)。
我从事 Java 工作已经很长时间了,大约 6 个月前开始使用 Scala。我喜欢这门语言。我发现的一件事是,做事有多种方法。我不知道这是因为该语言的性质,还是因为它还很年轻并且在不断发展,习惯用法和
这是我所指的示例代码。 https://sites.google.com/site/ssljavaguide/example-code/2-way-ssl 我是否可以不设置与 keystore 相关的
我读过有关使用 MySQL AES_ENCRYPT/AES_DECRYPT(双向加密)不如使用 PHP - hash()(单向加密)安全的信息。 http://bytes.com/topic/php/
我正在进行一个路线选择项目,我需要使用道路类型和单向/双向交通信息填充道路网络。我想知道Tiger/Line道路数据集是否包含这样的数据。。我下载了加利福尼亚州的Tiger/Line道路数据集,但没有
我需要开发一个 iPad 应用程序,它应该管理两种方向模式(横向和纵向)。 根据 official Apple iOS documentation , 有 2 种方法可以继续。 -第一个包括在收到旋转
我正在训练一个 randomForest 模型,目的是保存它以进行预测(它将被下载并在外部上下文中使用)。我希望这个模型尽可能最小。 我读到有很多options和 packages减少模型的内存大小。
为什么将参数传递给匿名函数会影响结果?例如,下面的脚本将 a1 显示为 function(),将 a2 显示为数组。 var a1=(function(){return [1*2,2*2,3*2];}
我有一个 Python 列表: listx = [["a", 127, "Blue", 0], ["b", 127, "Red", 1], ["b", 127, "
在查看 Java 库时,特别是构造函数,我注意到字段通常会出于某种原因进行初始化和验证: public java.awt.Color(int r, int g, int b, int a) {
我想编写 Git 脚本。只创建一个 Unix 脚本是最好的方法吗? #!/bin/bashgit push origin master &&git checkout develop &&git mer
这个问题在这里已经有了答案: class or method alias in java (8 个回答) 去年关闭。 我有一个类的名称可能不必要地繁琐,其中包含许多我在其他地方使用的静态方法。 而不是
这个问题在这里已经有了答案: Best way to check function arguments? [closed] (14 个回答) Parameter validation, Best pr
在阅读我遇到的代码时,结构的以下定义和初始化: // header file struct foo{ char* name; int value; }; //Implementation file s
我正在使用多页表单方法在 Drupal 中开发一个自定义模块,并且我希望对步骤进行可视化。 步骤 1 > [_Step_2_] > 步骤 3 > 完成 商业规则: 他们总是能看到所有的步骤,以及他们现
Josh 的 answer 给我留下了深刻的印象关于客户端的“Angular 方式”和声明式风格。 但是你能帮我理解一下,怎么做吗: 我有一个单页应用程序,左侧是菜单栏,右侧是 div 容器。 当用户
Subversion 商店正在考虑改用 Mercurial,试图提前弄清楚开发人员的所有提示将是什么。这里有一个相当常见的用例,我不知道如何处理。 我正在研究一些较大的功能,我有一个重要的代码部分——
我是一名优秀的程序员,十分优秀!