草庐IT

unnamed-namespace

全部标签

c++ - 为什么在类/结构级别不允许 "using namespace X;"?

classC{usingnamespacestd;//error};namespaceN{usingnamespacestd;//ok}intmain(){usingnamespacestd;//ok}我想知道背后的动机。 最佳答案 我不确切知道,但我的猜测是在类范围内允许这样做可能会导致混淆:namespaceHello{typedefintWorld;}classBlah{usingnamespaceHello;public:WorldDoSomething();}//ShouldthisbejustWorldorHello::

c++ - C++ 头文件中的 "using namespace"

这个问题在这里已经有了答案:Whyis"usingnamespacestd;"consideredbadpractice?(41个回答)关闭去年。在我们所有的c++类(class)中,所有老师总是将usingnamespacestd;放在他们的.h文件中的#include之后.在我看来,这似乎很危险,因为从那时起,通过将该header包含在另一个程序中,我会将命名空间导入到我的程序中,可能没有意识到、有意或想要它(header包含可以非常深入地嵌套)。所以我的问题是双重的:我是否正确usingnamespace不应该在头文件中使用,和/或有什么方法可以撤消它,例如://header.h

C++:嵌套模板类错误 "explicit specialization in non-namespace scope"

以下代码:templatestructA1{templatestructA2{/*...*/};templatestructA2{/*...*/};};intmain(){A1::A2x;}给出这个错误:prog.cpp:7:13:error:explicitspecializationinnon-namespacescope'structA1'prog.cpp:8:10:error:templateparametersnotusedinpartialspecialization:prog.cpp:8:10:error:'T1'如何最好地解决此错误?我试过这个:templatestru

C++:嵌套模板类错误 "explicit specialization in non-namespace scope"

以下代码:templatestructA1{templatestructA2{/*...*/};templatestructA2{/*...*/};};intmain(){A1::A2x;}给出这个错误:prog.cpp:7:13:error:explicitspecializationinnon-namespacescope'structA1'prog.cpp:8:10:error:templateparametersnotusedinpartialspecialization:prog.cpp:8:10:error:'T1'如何最好地解决此错误?我试过这个:templatestru

ruby - 在 Ruby 的模块中包含模块时如何解决 namespace 冲突?

这是我的代码:moduleAdeffooputs"A"endendmoduleBincludeAdefbarfooendendclassMyClassincludeBdeffooputs"X"enddefself.testputsbarendend当我调用“C.test”时,我得到的是“X”而不是“A”(这是我想要的),因为foo的本地定义已经覆盖了A中的定义。我无法更改任何一个foo的签名.我只能主要编辑自己的类(class);我可以编辑模块A和B,但许多现有代码都在使用它们,而且它们确实如此(例如,无需将foo更改为A.foo)。我正在考虑做classMyClassmoduleMy

ruby-on-rails - JSONAPI 资源 : serialize a namespaced model

我正在尝试使用JSONAPIResources在Rails引擎中,我定义了DokiCore::Tenant(模型)在doki_core/app/models/tenant.rb和DokiCore::TenantResource在doki_core/app/resources/tenant_resource.rb中。当我尝试序列化为哈希时,遇到以下错误:NoMethodError:undefinedmethodtenant_path'for#from/Users/typeoneerror/.rvm/gems/ruby-2.2.2@doki/gems/jsonapi-resources-0

ruby-on-rails - Rails - 使用 namespace Controller 来组织文件

我正在尝试学习命名空间。我之前就这个主题问过几个问题,但我不明白发生了什么。我在Controller的文件夹中创建了一个名为“功能”的文件夹。在其中,我保存了一个名为app_roles_controller.rb的文件。该Controller的第一行是:classFeatures::AppRolesControllerfeatures文件夹的目的是让我可以更好地组织我的文件(仅此而已)。在我的routes.rb中,我尝试过:resources:app_roles,:namespace=>"features",:controller=>"app_roles"我也试过:namespace:

ruby - "pollute the global namespace"是什么意思?

在ruby​​中,一些gems选择“污染全局命名空间”。这是什么意思?我如何才能看到它发生在哪里?为什么gem需要这样做?当面对污染全局命名空间和冲突的两个gem时,当我选择“隔离”一个时,我要做出哪些权衡?例如:我正在使用两个污染全局命名空间的gem:pry和gli所以我无法再将我的binding.pry放在我想要的地方。一个solution就是将整个cli包装在一个模块中:moduleWrapperincludeGLI::Appextendselfprogram_desc"..."...exitrunARGVend现在我可以在任何需要的地方使用我的binding.pry。为什么这样

ruby - rake db:create:ll 中#<Jquery::Rails::Engine:0x102530798> 的未定义方法 `namespace'

运行rakedb:create:all时出现以下错误:rakeaborted!undefinedmethod`namespace'for#rakefile如下所示:requireFile.expand_path('../config/application',__FILE__)module::TestProjectclassApplicationincludeRake::DSLendendmodule::RakeFileUtilsextendRake::FileUtilsExtendTestProject::Application.load_tasks这里出了什么问题?

ruby-on-rails - Rails 与 namespace 的多态关联

我想保存不同的结果(默认和手动),每个结果都可以有一个原因。认为这将是多态关联的好地方。然而,模型是命名空间的,事实证明这比预期的要复杂。以下theguideapp/models/event/reason.rb#id:integernotnull,primarykey#reasons:string#reasonable_id:integer#reasonable_type:string#created_at:datetimenotnull#updated_at:datetimenotnull#classEvent::Reasonapp/models/event/result.rbcla