Java通用问题(Java Generic Question)
下面的代码编译,但如果我取消注释注释行,它不会,我很困惑为什么。 HashMap确实扩展了AbstractMap,并且声明map的第一行编译正常。
import java.util.AbstractMap; import java.util.HashMap; import java.util.Map; public class Test { public static void main(String args[]) { Map<String, ? extends AbstractMap<String, String>> map = new HashMap<String, HashMap<String, String>>(); //map.put("one", new HashMap<String, String>()); } }
而且,我知道“正确的方式”是这样的:
import java.util.HashMap; import java.util.Map; public class Test { public static void main(String args[]) { Map<String, Map<String, String>> map = new HashMap<String, Map<String, String>>(); map.put("one", new HashMap<String, String>()); } }
The following code compiles but if I uncomment the commented line, it does not and I am confused why. HashMap does extend AbstractMap and the first line where map is declared compiles fine.
import java.util.AbstractMap; import java.util.HashMap; import java.util.Map; public class Test { public static void main(String args[]) { Map<String, ? extends AbstractMap<String, String>> map = new HashMap<String, HashMap<String, String>>(); //map.put("one", new HashMap<String, String>()); } }
And, I know the "right way" is this:
import java.util.HashMap; import java.util.Map; public class Test { public static void main(String args[]) { Map<String, Map<String, String>> map = new HashMap<String, Map<String, String>>(); map.put("one", new HashMap<String, String>()); } }
原文:https://stackoverflow.com/questions/5974065
满意答案
你是对的。 检测无限递归而不限制模板元编程上的递归堆栈帧意味着找到停止问题的替代解决方案。
理论上有几种特殊情况是可检测的。 例如,如果您可以确保递归的引用透明性,并且最后一个函数调用接收到与实际调用相同的参数,那么您将进行无限递归调用。 C ++不提供模板元编程的参考透明度保证。
Your are correct. Detecting an infinite recursion without limiting the recursion stack frames on template meta programming would mean finding an alternative solution to the halting problem.
There are a few special cases which are, in theory, detectable. For example, if you can ensure referential transparency on the recursion and if the last function call receives the same parameters as the actual one, you are on an infinite recursive call. C++ offers no referential transparency warranty on template meta programming.
相关问答
更多模板(元)编程总是只有一种实现方式吗?(Does template (meta)programming has always only one way of implementation?)
什么样的C ++模板编程可以称为“元编程”?(What kind of C++ template programming can be called “meta programming”? [closed])
C ++模板元编程:重载运算符(C++ template meta programming: overloading operators)
编译器如何理解递归?(How do compilers understand recursion?)
使用成员函数指针进行模板元编程?(Template meta-programming with member function pointers?)
C ++模板元编程:从模板模板参数继承(C++ Template Meta Programming: Inheritance from template template parameter)
如何在c ++中编写元编程模板的最后一个递归(How to write last recursion for meta-programming template in c++)
模板元编程,使用可变模板:编译器错误(Template Meta-programming, with Variadic Templates: compiler error)
编译器是否可以在模板元编程中识别递归问题?(Can compilers identify recursion issues in Template Meta programming?)
编译器是否保证优化模板生成的递归代码?(Are compilers guaranteed to optimize recursive code generated by templates?)
相关文章
更多Solr Cache使用介绍及分析,包括LRUCache、filterCache、queryResultCache、documentCache、Generic Caches
java通用返回对象
《JAVA代码规范》(四)通用代码格式 - 注释(2.7)
《JAVA代码规范》(二)通用代码格式 - 文件组织(2.1)
《JAVA代码规范》(五)通用代码格式 - 声明(2.8)
java该词,国内程序员和国外程序员念法一样吗,通用念法是什么?
《JAVA代码规范》(三)通用代码格式 - 缩进、行长度、换行、空行、空格(2.2-2.6)
《JAVA代码规范》(六)通用代码格式 - 语句、SQL(2.9-2.10)
Hadoop Java程序-files功能测试
最新问答
更多获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
如何通过引用返回对象?(How is returning an object by reference possible?)
矩阵如何存储在内存中?(How are matrices stored in memory?)
每个请求的Java新会话?(Java New Session For Each Request?)
css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
xcode语法颜色编码解释?(xcode syntax color coding explained?)
在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
Copyright ©2023 peixunduo.com All Rights Reserved.粤ICP备14003112号
本站部分内容来源于互联网,仅供学习和参考使用,请莫用于商业用途。如有侵犯你的版权,请联系我们(neng862121861#163.com),本站将尽快处理。谢谢合作!