首页 \ 问答 \ 模板const /非const参数强制转换(Template const/non-const argument cast)

模板const /非const参数强制转换(Template const/non-const argument cast)

我经历了一些搜索但却找不到这个搜索。 考虑这种情况:

template <class T> class TemplClass;
void a_function(TemplClass<const X>&);

TemplClass<X> inst;
a_function( inst ); // fails

“从'TemplClass'类型的表达式''TemplClass&'类型的引用无效初始化”

据我所知,这种情况是100%安全的。 尽管如此,C ++仍然不允许这样做。 所以我想知道使用什么演员而不是琐碎的C-cast。

a_function( static_cast<TemplClass<const X>&>(inst) ); // fails, similar error message

a_function( reinterpret_cast<TemplClass<const X>&>(inst) ); // works

dynamic_cast是不可能的,const_cast也失败了(这是正确的)。

reinterpret_cast感觉很腥(是吗?)。 但有没有我错过的某种技巧的解决方案? 任何人都知道为什么标准不能简单地发现这是好事吗? 或者这个演员有什么“坏”吗?


I went through some searches but couldn't quite find this one. Consider this situation:

template <class T> class TemplClass;
void a_function(TemplClass<const X>&);

TemplClass<X> inst;
a_function( inst ); // fails

"invalid initialization of reference of type ‘TemplClass&’ from expression of type ‘TemplClass’"

The situation is 100% safe as far as I can tell. Still, C++ does not allow this. So I wonder what cast to use instead of the trivial C-cast.

a_function( static_cast<TemplClass<const X>&>(inst) ); // fails, similar error message

a_function( reinterpret_cast<TemplClass<const X>&>(inst) ); // works

dynamic_cast is out of the question, const_cast fails too (and rightly so).

The reinterpret_cast feels fishy (is it though?). But is there a solution with some kind of trick that I missed? Anyone know why the standard does not simply detect that this is something good? Or is there something 'bad' about this cast?


原文:https://stackoverflow.com/questions/36178920
更新时间:2023-09-16 20:09

最满意答案

我在你的笑脸图片中添加了属性draggable="true"和ID id="myImg"
然后,如果您将dataTransfer事件属性设置并获取到originalEvent ,它可以正常工作。
;)

$(".dropzone").on("dragover", function(ev) {
  ev.preventDefault();
});

$(".dropzone").on("drop", function(ev) {
  ev.preventDefault();
  var data = ev.originalEvent.dataTransfer.getData("text");
  ev.target.appendChild(document.getElementById(data));
});

$(".dragable").on("dragstart", function(ev) {
  ev.originalEvent.dataTransfer.setData("text", ev.target.id);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="dropzone" style="width: 180px; height: 180px; padding: 10px; margin: 0px; border: 1px solid black;"></div>
<br>
<div class="dropzone" style="width: 180px; height: 180px; padding: 10px; margin: 0px; border: 1px solid black;">
  <img class="dragable" draggable="true" id="myImg" src="https://image.spreadshirtmedia.net/image-server/v1/compositions/22839312/views/1,width=300,height=300,version=1478003241/smiley-smile-men-s-t-shirt.jpg" width="180">
</div>


I added the propertie draggable="true" and an ID id="myImg" to your smiley image.
Then, if you set and get the dataTransfer event property into the originalEvent, it works fine.
;)

$(".dropzone").on("dragover", function(ev) {
  ev.preventDefault();
});

$(".dropzone").on("drop", function(ev) {
  ev.preventDefault();
  var data = ev.originalEvent.dataTransfer.getData("text");
  ev.target.appendChild(document.getElementById(data));
});

$(".dragable").on("dragstart", function(ev) {
  ev.originalEvent.dataTransfer.setData("text", ev.target.id);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="dropzone" style="width: 180px; height: 180px; padding: 10px; margin: 0px; border: 1px solid black;"></div>
<br>
<div class="dropzone" style="width: 180px; height: 180px; padding: 10px; margin: 0px; border: 1px solid black;">
  <img class="dragable" draggable="true" id="myImg" src="https://image.spreadshirtmedia.net/image-server/v1/compositions/22839312/views/1,width=300,height=300,version=1478003241/smiley-smile-men-s-t-shirt.jpg" width="180">
</div>

相关问答

更多
  • RealmResult不是列表,因此您无法在RealmResult交换项目。 你可以做的是 List list = realm.copyFromRealm(realmResults); 但是,这将使列表不受管理,这意味着如果更新数据库中的数据,对象将不会自动更新。 A RealmResult is not a list and hence you cannot swap the items in RealmResult. What you can do is List li ...
  • 我相信你的问题是, 响应者链中的某些东西正在处理-[draggingEntered:]并拒绝拖动,然后你的窗口才能到达它。 对于一个典型的AppKit应用程序,在进入窗口之前,一个动作消息从NIB转到第一响应者,任何东西都挂在它的背面,然后是最里面的视图及其委托,然后是它的所有祖先视图及其委托。 因此,例如,如果您有一个处理拖动消息的文本编辑视图,并且您拖动该视图,则窗口将不会看到它。 无论如何,有很多方法可以调试它,但最简单的方法是让每个方法从self , print ing(或logging.log g ...
  • 您可以像这样设置数据: e.originalEvent.dataTransfer.setData('Text', this.model.id); 这是因为Backbone使用jQuery挂钩事件, e是一个jQuery事件对象。 You can set the data like this: e.originalEvent.dataTransfer.setData('Text', this.model.id); This is because Backbone is hooking up the event ...
  • 假设您的意思是使用动态创建的元素进行拖动,我已经更新了您的jsfiddle。 http://jsfiddle.net/7c3v0s1s/6/我在进行更改时将代码包装在命名空间中。 HTML
    使用Javascript var localNameSpace = { dragSrcEl: null , bindDraggables: function() { ...
  • 我解决了这个问题。 我对鼠标事件进行了更改 private void dataGridView1_MouseMove(object sender, MouseEventArgs e) { if ((e.Button & MouseButtons.Left) == MouseButtons.Left) { if (dragBoxFromMouseDown != Rectangle.Empty && !dragBox ...
  • 目前还没有内置的交互 ,但Serenity / JS很容易扩展,因此您可以创建自定义交互(甚至可以将其作为拉取请求提交?)。 以下是我创建自定义交互的步骤。 1.研究量角器的方式 首先,考虑如何使用普通的Protractor实现此功能? 量角器的API文档提供以下选项: // Dragging one element to another. browser.actions(). mouseDown(element1). mouseMove(element2). mouseUp(). ...
  • 这实际上不是由库或侦听器引起的,它是由位于相同布局中的EditText引起的。 它是通过创建一个子类EditText并忽略dragEvent的新类来解决的。 public class EditTextNoDrag extends EditText { public EditTextNoDrag(Context context) { super(context); } public EditTextNoDrag(Context context, AttributeSet attrs) ...
  • 我在你的笑脸图片中添加了属性draggable="true"和ID id="myImg" 。 然后,如果您将dataTransfer事件属性设置并获取到originalEvent ,它可以正常工作。 ;) $(".dropzone").on("dragover", function(ev) { ev.preventDefault(); }); $(".dropzone").on("drop", function(ev) { ev.preventDefault(); var data ...
  • 你可以添加一个计数器并检查它是否等于document.querySelectorAll('[data-drop]').length : var counter = 0; function dropPic(ev) { ev.preventDefault(); var data = ev.dataTransfer.getData('text'); //make it to only drop in certain DIV attribute if (ev.targ ...
  • 刚刚做了一些搜索,并找到了如何解决它。 在NSTreeController的Attributes中,将fetch谓词设置为parent == nil 。 Just did a Bit of searching around, and found how to fix it. In the Attributes for the NSTreeController set the fetch predicate as parent == nil.

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)