在类中定义的友元函数的完全限定名称是什么?
我最近看到了一个类似于以下的例子.以下完全限定名称是val()什么?
#include <iostream>
namespace foo {
class A {
int x;
public:
A(int x = 0) : x(x) { }
friend int val(const A &a) { return a.x; }
};
}
int main() {
foo::A a(42);
// val() found using ADL:
std::cout << val(a) << std::endl;
// foo::val(a); // error: 'val' is not a member of 'foo'
// foo::A::val(a); // error: 'val' is not a member of 'foo::A'
return 0;
}
Run Code Online (Sandbox Code Playgroud)
参数依赖查找是唯一val()可以找到的方法吗?
不可否认,这并非源于实际问题.我只是希望获得更好的理解.
在文本中匹配完全限定的Java类名称的最佳方法是什么?
例如:java.lang.Reflect,java.util.ArrayList,org.hibernate.Hibernate.
基本上,何时真正需要(如果有的话)使用完全限定的xml参见参考:
<see cref="T:MyNamespace.Sub.MyType"/> //Option 1
<see cref="T:MyType"> //Option 2
Run Code Online (Sandbox Code Playgroud)
另外,引用.NET Framework对象怎么样?
<see cref="T:System.Collections.Generic.ICollection{T}"/> //Option 1
<see cref="T:ICollection{T}"/> //Option 2
Run Code Online (Sandbox Code Playgroud)
我知道完全符合条件的项目将始终允许Microsoft的Sandcastle正确地链接事物,但是一切都必须完全合格吗?
旁注:Microsoft Sandcastle是否能够链接到.NET Framework帮助文件,还是我通过引用浪费时间<see cref="T:System.Collections.Generic.ICollection{T}"/>?
该程序导致不希望的贪婪解析的死胡同:
struct float4x4 {};
class C
{
float4x4 M();
};
float4x4 ::C::M()
{
return float4x4{};
}
Run Code Online (Sandbox Code Playgroud)
:8:1:错误:'float4x4'中没有名为'C'的成员; 您的意思仅仅是“ C”吗?
float4x4 :: C :: M()
^ ~~~~~~~~~~~~
可以使用尾随返回类型“固定”:
auto ::C::M() -> float4x4
{}
Run Code Online (Sandbox Code Playgroud)
现在一切都好。
因此,我认为在使用heading-return-type声明符语法时,我们不能完全限定类名吗?
今天偶然发现了这个,我找不到任何有关它的信息.所以这就是我在这里问的原因.也许有人知道为什么.
我在web.config中添加了一个自定义WCF行为扩展.它看起来像这样:
<behaviorExtensions>
<add name="errorBehavior" type="MyNs.TracingErrorBehaviorElement,MyNs,
Version=1.0.6.0, Culture=neutral, PublicKeyToken=null" />
</behaviorExtensions>
Run Code Online (Sandbox Code Playgroud)
(在那里没有足够的空间:MyNs.TracingErrorBehaviorElement,MyNs)
它在我的开发机器,我们的登台服务器,我们的实时服务器等上运行得非常好.
今天我们在客户服务器上安装了产品,并得到以下例外:
System.Configuration.ConfigurationErrorsException:为system.serviceModel/behavior创建配置节处理程序时发生错误:无法将扩展元素"errorBehavior"添加到此元素.验证扩展是否已在system.serviceModel/extensions/behaviorExtensions扩展集合中注册...
花了半个小时在网上搜索可能的原因后,我在完全限定的程序集名称中添加了空格.所以我改成了:
<behaviorExtensions>
<add name="errorBehavior" type="MyNs.TracingErrorBehaviorElement, MyNs,
Version=1.0.6.0, Culture=neutral, PublicKeyToken=null" />
</behaviorExtensions>
Run Code Online (Sandbox Code Playgroud)
(参见空间:MyNs.TracingErrorBehaviorElement, MyNs)
它起作用了.
有谁知道为什么它在某些机器上没有空间而不在其他机器上工作?我查了一下.Net-versions.他们匹配.它可能是由区域设置引起的吗?
编辑说:
我检查了所有机器上使用过的.Net版本,它们都是一样的:.Net 4.0但是我发现错误的机器和缺少的空白机器之间的区别是:它运行的所有机器没有空白已安装.Net Framework 4.5.所以它可能是4.0中修复并用4.5部署的那些错误之一,对吧?
在此示例代码中,有两个句子显示相同的静态变量.第一个没有歧义,但第二个没有,为什么?
#include <iostream>
using namespace std;
struct A { static const char a = 'a'; };
struct B : public A { };
struct C : public A { };
struct G : public B, public C { };
int main()
{
G v;
cout << G::B::A::a << endl;
cout << v.B::A::a << endl;
}
Run Code Online (Sandbox Code Playgroud)
GCC错误(根据一些评论,clang中没有歧义):
main.cpp:15:18: error: 'A' is an ambiguous base of 'G'
cout << v.B::A::a << endl;
Run Code Online (Sandbox Code Playgroud)
这种情况对于你们中的一些人来说可能并不完全不常见:你们有一些功能要放在一个类中,但是这个类的完美名称(*)是由System命名空间中的一个类或其他不属于你的命名空间/类所占用的.你在using/ import.
(*)完美我的意思是小巧,简洁和清晰的名字.
例如,我有一个Utils类Diagnostics(主要是debug utils)类和一个Drawing类.我可以:
DrawingUtils类和一个DiagnosticsUtils类,但这只是闻起来像坏结构.我认为选项1-3并不乐观:(
由于我选择的答案并没有明确地解决问题(我也不这样做),我建议那些面临同样情况的人要问自己:你会经常使用冲突的BCL类/命名空间吗?如果不是,那么让你的名字发生冲突(就像我在诊断中所做的那样).如果是,请添加一个限制类/命名空间可能性的单词.
在实践中,这意味着::
"Drawing"绘制的东西.
"MyCustomControlDrawing":东西借鉴只上MyCustomControl.例如:"WidgetDrawing".
c# vb.net naming-conventions classname fully-qualified-naming
我有一个MyObject带有property(MyProperty)的object ().我想要得到它的类型名称(即String或MyClass等).我用:
PropertyInfo propInfo = typeof(MyObject).GetProperty("MyProperty");
Console.WriteLine(propInfo.PropertyType.Name);
Console.WriteLine(propInfo.PropertyType.FullName);
Run Code Online (Sandbox Code Playgroud)
简单类型没有问题,但是当MyProperty它是泛型类型时,我在获取它的名字方面遇到了问题(例如Collection<String>).它打印:
Collection`1
System.Collections.ObjectModel.Collection`1 [[System.String,mscorlib,Version = 2.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089]]
那是什么`1?我怎样才能获得" Collection<String>"?
我想获取T使用它的类型名称:
typeof(T).Name
Run Code Online (Sandbox Code Playgroud)
这个班的名字是 ConfigSettings
而不是返回ConfigSettings它返回ConfigSettings`1.
有什么具体原因吗?如何在没有的情况下返回实际名称`1?
不太确定最好的头衔,但我会尽我所能解释我的要求.假设我有以下文件:
MyCustomClass.php
<?php
namespace MyNamespace;
use FooNamespace\FooClass;
use BarNamespace\BarClass as Bar;
use BazNamespace\BazClass as BazSpecial;
class MyCustomClass {
protected $someDependencies = [];
public function __construct(FooClass $foo, Bar $bar) {
$someDependencies[] = $foo;
$someDependencies[] = $bar;
}
}
Run Code Online (Sandbox Code Playgroud)
现在,如果我要使用反射,我可以从构造中的类型提示中获取完全限定的类名.
但是,我会接受FooNamespace\FooClass和BarNamespace\BarClass.不,FooNamespace\FooClass和BarNamespace\Bar.我也没有参考BazNamespace\BazClass.
基本上,我的问题是:我怎样才能得到完全合格的名称MyCustomClass.php,而只知道FooClass,Bar和,BazSpecial?
我不想使用文件解析器,因为这会吃掉性能.我希望能够做到这样的事情:
$class = new ReflectionClass('MyCustomClass');
...
$class->getUsedClass('FooClass'); // FooNamespace\FooClass
$class->getUsedClass('Bar'); // BarNamespace\BarClass
$class->getUsedClass('BazSpecial'); // BazNamespace\BazClass
Run Code Online (Sandbox Code Playgroud)
我该怎么做呢?
c# ×5
c++ ×3
.net ×2
generics ×2
namespaces ×2
reflection ×2
class ×1
classname ×1
friend ×1
java ×1
matching ×1
methods ×1
name-lookup ×1
parsing ×1
php ×1
regex ×1
sandcastle ×1
vb.net ×1
xml-comments ×1