- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试为类的模板化转换运算符指定模板参数,但我似乎无法获得正确的语法。
#include <iostream>
using namespace std;
class C
{
int i_;
public:
C(int i) : i_(i) {}
template<int adder> int get() { return i_ + adder; }
template<int adder> int operator()() { return i_ + adder; }
template<int adder> operator int() { return i_ + adder; }
// If I add a default argument to operator int()'s adder template parameter this compiles fine
// (of course, I still can't figure out how to specify it...)
};
int main(int, char*[])
{
C c(10);
cout << c.get<2>() << endl; // I can specify template argument here the regular way.
// cout << c() << endl; // No template argument specified, so I wouldn't have expected this to work.
cout << c.operator()<3>() << endl; // We have to call it this way.
// cout << (int)c << endl; // In the same vein I wouldn't expect this to work either.
cout << c.operator int<4>() << endl; // But how do I specify template argument here? This seems to be an error for some compilers.
return 0;
}
相同的代码在 http://liveworkspace.org/code/35sqXe$4
使用 g++ 4.7.2 编译时
$ g++ -std=c++11 -Wall -W -pedantic "template conversion operator.cpp"
Compilation finished with errors:
source.cpp: In function 'int main(int, char**)':
source.cpp:23:23: error: 'int' is not a template
source.cpp:23:30: error: no matching function for call to 'C::operator int()'
source.cpp:23:30: note: candidate is:
source.cpp:11:24: note: template<int adder> C::operator int()
source.cpp:11:24: note: template argument deduction/substitution failed:
source.cpp:23:30: note: couldn't deduce template parameter 'adder'
使用 g++ 4.8.0 (20130224) 编译时
$ g++ -std=c++11 -Wall -W -pedantic "template conversion operator.cpp"
Compilation finished with errors:
source.cpp: In function 'int main(int, char**)':
source.cpp:23:23: error: 'int' is not a template
cout << c.operator int<4>() << endl;
^
source.cpp:23:30: error: no matching function for call to 'C::operator int()'
cout << c.operator int<4>() << endl;
^
source.cpp:23:30: note: candidate is:
source.cpp:11:24: note: template<int adder> C::operator int()
template<int adder> operator int() { return i_ + adder; }
^
source.cpp:11:24: note: template argument deduction/substitution failed:
source.cpp:23:30: note: couldn't deduce template parameter 'adder'
cout << c.operator int<4>() << endl;
^
使用 clang++ 3.2 编译时
$ clang++ -std=c++11 -Wall -W -pedantic "template conversion operator.cpp"
Compilation finished with errors:
source.cpp:23:12: error: reference to non-static member function must be called
cout << c.operator int<4>() << endl;
^~~~~~~~~~~~~~
source.cpp:23:30: error: expected expression
cout << c.operator int<4>() << endl;
^
2 errors generated.
使用 icc 13.0.1 编译时
$ icc -std=c++11 -Wall -W -pedantic "template conversion operator.cpp"
Compilation finished with warnings:
source.cpp(11): warning #488: constant "adder" is not used in declaring the parameter types of function template "C::operator int"
template<int adder> operator int() { return i_ + adder; }
^
除警告外,icc 似乎工作正常。
这些是编译器错误吗?还是我的语法有问题?
编辑
自从 Yakk 问我原来/实际的问题是什么:
我有一个 Ptr 类(以它指向的类型为模板),我想将 Ptr 转换为 const T。(虽然我知道在这种情况下这并不重要,)我希望转换运算符不如果 T 已经是 const 类型,就在那里。由于您没有为转换运算符指定返回类型或方法参数,因此我将 enable_if 作为方法模板参数的一部分。
正如 Yakk(和其他问题中的其他人)发布的那样,一个简单的 template <typename = typename std::enable_if<!std::is_const<T>::value>::type>
不起作用,因为当实例化 Ptr 时,T 在编译器到达此声明时已知。由于没有推导 T,因此不存在 SFINAE。因为我们知道!is_const<T>::value
为假,没有“类型”成员,声明无效。使模板依赖于新类型 (U),推导 U,然后检查 U 是否与 T 相同,以及 T 不是 const,然后进行无效声明是 SFINAE 的有效使用并且有效正如预期的那样。
template <typename T>
class Ptr
{
template <typename U,
typename = typename std::enable_if<std::is_same<T, U>::value &&
!std::is_const<U>::value>::type>
operator Ptr<const U>() const { return active; }
};
但后来我对自己说,这是一个模板化的成员函数。这些模板参数不必保留为默认值,任何实例化该函数的人都可以指定它们。对于任何其他 operator xxx 函数,执行此操作的语法很明显并且有效(请参阅上面的 operator())。对于这个例子:
Ptr<const int> ci;
ci.operator Ptr<const int><const int, void>(); // assuming this syntax is valid
void(或那里的任何其他类型)将指定转换运算符的第二个模板参数,并且不会考虑包含 enable_if 的默认值。当我试图使它不存在时,这将使该方法存在。
但是 gcc、clang 和 msvc 似乎对这种语法有问题。我假设因为转换运算符拼写为 operator typename
,具有模板参数会使编译器感到困惑,认为它们是用于类型名而不是运算符。
确实有变通办法(只包括转换运算符,当 T 已经是 const 时转换为 const T 不会造成任何伤害),但那是针对这个特定问题的。也许无法为转换运算符指定模板参数,因此,让这些类型被推导/默认就可以了。或者也许它有一个语法(icc 似乎接受它......),所以我向用户开放自己指定模板参数和实例化我不想要它们的方法。我已经有了针对我的特定问题的解决方案(在类型确实重要的时候,在转换运算符的类型检查中使用 static_assert),但这个问题是关于 C++ 语言及其语法的。顶部的 C 类只是我能想到的搜索该语法的最简单方法。
最佳答案
有点不清楚您要实现的目标...通常没有充分的理由让所有这些成员函数成为模板,您也可以将它们设为采用 adder
的常规函数。作为论点。
get
函数,好吧,并不是真正的获取,而是添加,所以你可以称它为add
.函数调用运算符 operator()()
很可能需要一个int
作为论据。转换运算符为 int
从字面上看,作为模板没有任何意义,并且不能按定义调用。如果你坚持要 get
和 operator()
作为模板,您可以将它们称为:
C c(0);
c.get<5>(); // 5
c<5>(); // 5
但我建议您重新考虑设计,确定您真正需要什么以及模板是否可行...(请注意,即使在非模板版本中,转换为int
采用值,您不是转换,而是创建一个不同的 int
!)
关于c++如何为类的模板化转换运算符指定参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15305891/
namespace std { template <> class hash{ public : size_t operator()( cons
我正在构建一个 Javascript 交互性有限的 Django 应用程序,并且正在研究如何将 Vue 模板与 Django 模板合并以实现相同的内容。 想象一个无限滚动的页面,其中 SEO 非常重要
我需要一个由游戏逻辑组成的外部类,调用 LitElement 组件,并向其传递一个 html 模板文字,该组件将使用该文字来更新其自己的 html 模板文字的一部分。 在下面的代码中,您将看到组件的一
很简单,我不想在 html 文件中定义所有 Handlebars 模板 我试过了 但这并没有奏效。我是否可以不以编程方式定义模板,甚至只是加载 Handlebars 文件,以便我可以重用,而且我觉得
在此代码中,j 正确地成为对象:j.name、j.addr、j.city、j.state 和 j.zip。但是,成功函数有一个 JavaScript 错误 .tmpl() 不是函数。 {{t
Django模板不会?点进来,总结了模板语法传值取值、过滤器和自定义过滤器、模板标签的分类、中间件403报错如何解决、如何继承模板~👆 Django 模板 模板传值取值 后端传值 键值对形式:{‘n
哈喽大家好,我是鹿 九 丸 \color{red}{鹿九丸}鹿九丸,今天给大家带来的是C++模板。 如果大家在看我的博客的过程中或者学习的过程中以及在学习方向上有什么问题或者想跟我交流的话可以加我的企
我正在用 PHP 编写一个简单的模板层,但我遇到了一些困难。目前它是这样工作的: 首先,我使用 fetch_template 从数据库中加载模板内容 - 这可行(如果您有兴趣,我会在启动时收集所有模板
我正在制作有关模板的 Django 教程。我目前处于此代码: from django.template import Template, Context >>> person = {'name': '
我正在使用 Jquery 模板来显示传入的 JSON 数据我想将模板加载到可缓存的外部文件中。我该怎么做? 更新 http://encosia.com/2010/12/02/jquery-templa
这是我的观点.py: from django.http import HttpResponse from django.template.loader import get_template from
我试图说服一位同事在项目的前端使用 Mustache/Hogan,我提出了以下建议: 有一个 templates.js 文件,大致如下所示: var tpl_alert = '{{msg}}'; va
我想创建一个通用的数组函数。在我的 API 中,我有一个通用容器,我需要将其转换为正确的类,但我想让它通用 template void UT::printArray(CCArray* arr, T t
有谁知道是否有办法在 Genshi 中创建 javascript 模板?我的意思是,我需要一个 .js 文件,可以在其中使用 等指令。等等。 有什么想法吗?谢谢! 最佳答案 你可以直接在html中这
我想知道是否可以设置某种 HTML 模板系统,基本上我有 3 个不同的文件: - header.html - footer.html - landing.html(landing.html 是包含页面
我正在尝试构建以下 HTML 模板: 这很简单,如果我使用红色容器 1-4,语法如下: 1 2 3 4 5 6 7 8 9 https://jsfi
#include "boost/numeric/ublas/matrix.hpp" using namespace boost::numeric::ublas; template class Lay
我在一个类中有一个函数,它传递了一个函数及其参数,然后将它们绑定(bind)到一个函数调用中并调用该函数等。 这已经被快速组合在一起以测试我知道代码不是很好的概念。 class Profiling {
是否有一个 c++ 结构或模板(在任何库中)允许我在十进制和任何其他基数之间进行转换(很像 bitset 可以做的)? 最佳答案 是的,你可以使用unsigned int: unsigned int
来自其他编程语言,许多像我一样的人会感到惊讶。我有一个简单的问题。我有一个列表——比如说,用户。我想遍历用户并显示一些信息。非常简单,直到我被这个难住了: 使用一个 eex 模板,我试图这样做:
我是一名优秀的程序员,十分优秀!