我创建了一个mvc模型类,其中一个属性是'MyObject'类型.它还有一个System.ComponentModel.DataAnnotations.StringLength属性.
MyObject作为隐式转换运算符,因此它基本上可以用作字符串:
public static implicit operator string(MyObject o){...}
public static implicit operator MyObject(string sValue){...}
Run Code Online (Sandbox Code Playgroud)
出于某种奇怪的原因,这是一个asp mvc问题吗?我问,因为我知道在大多数情况下隐式转换工作正常,我可以例如将该属性分配给字符串值,它可以正常工作.
编辑-好吧,我知道为什么错误是发生:
这是因为StringLength.IsValid()方法需要一个对象作为参数,所以中投实际上是从对象去字符串,而不是从MyObject的串,所以这可以解释为什么我的隐式强制转换运算符未被调用.但是如何解决这个问题呢?
这一切都正常,直到我在我的模型中的属性上放置System.ComponentModel.DataAnnotations.StringLength属性,然后当视图从提交按钮发出帖子时,我得到了异常:
[InvalidCastException:无法将类型为'StrataSpot.Shared.Models.Email'的对象强制转换为'System.String'.]
System.ComponentModel.DataAnnotations.StringLengthAttribute.IsValid(Object value)+34
System.Web.Mvc.d__1. MoveNext()+56 System.Web.Mvc.DefaultModelBinder.OnPropertyValidated(ControllerContext controllerContext,ModelBindingContext bindingContext,PropertyDescriptor propertyDescriptor,Object value)+203 System.Web.Mvc.DefaultModelBinder.BindProperty(ControllerContext controllerContext,ModelBindingContext bindingContext,PropertyDescriptor propertyDescriptor)+413
System.Web.Mvc.DefaultModelBinder.BindProperties(ControllerContext controllerContext,ModelBindingContext的BindingContext)90
System.Web.Mvc.DefaultModelBinder.BindComplexElementalModel(ControllerContext controllerContext,ModelBindingContext的BindingContext,对象模型)383
System.Web.Mvc.DefaultModelBinder.BindComplexModel(ControllerContext controllerContext,ModelBindingContext bindingContext)+1048
System.Web .Mvc.DefaultModelBinder.BindModel(ControllerContext controllerContext,ModelBindingContext bindingContext)+280
System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ControllerContext controllerContext,ParameterDescriptor parameterDescriptor)+257
System.Web.Mvc.ControllerActionInvoker.GetParameterValues(ControllerContext controllerContext,ActionDescriptor actionDescriptor)+ 109
System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext,字符串actionName)314 System.Web.Mvc.Controller.ExecuteCore()105 System.Web.Mvc.ControllerBase.Execute(RequestContext的RequestContext的)39
的System.Web .Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext)+7
System.Web.Mvc.<> c__DisplayClass8.b__4()+ 34 System.Web.Mvc.Async.<> c__DisplayClass1.b__0() 21 System.Web.Mvc.Async.<> c__DisplayClass81.<BeginSynchronous>b__7(IAsyncResult _) +12 System.Web.Mvc.Async.WrappedAsyncResult1.End()59 System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult的asyncResult)44
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndPro cessRequest(IAsyncResult的结果)7
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()8678910 System.Web.HttpApplication.ExecuteStep(IExecutionStep步骤,布尔逻辑completedSynchronously)155
这里发生了什么:
printf("result = %d\n", 1);
printf("result = %f\n", 1);
Run Code Online (Sandbox Code Playgroud)
输出:
result = 1
result = 0.000000
Run Code Online (Sandbox Code Playgroud)
如果我在尝试打印它们之前确保这些变量的类型,它当然可以正常工作.为什么第二个print语句没有被隐式转换为1.00000?
我看到了一些示例,其中转换函数T => S作为隐式参数传递.Scala 调用此函数view甚至提供特殊的语法糖view bound- 对于这种情况.
但是我们已经进行了隐式转换!我可以views用隐式转换替换这些(即作为隐式参数传递的转换函数)吗??我可以views用隐含的转换来做什么?
我正在尝试解决缺少的问题,collection.mutable.SortedSet这就是我的跳过列表实现.我几乎在那里:
import collection.{SortedSet => CSortedSet, SortedSetLike => CSortedSetLike}
import collection.mutable.{Set => MSet, SetLike => MSetLike}
import collection.generic.{MutableSetFactory, GenericCompanion}
object SkipList extends MutableSetFactory[SkipList] {
def empty[A](implicit ord: Ordering[A]): SkipList[A] = ???
}
trait SkipList[A] extends MSet[A] with MSetLike[A, SkipList[A]] with CSortedSet[A]
with CSortedSetLike[A, SkipList[A]] {
override def empty: SkipList[A] = SkipList.empty[A](ordering)
def rangeImpl(from: Option[A], until: Option[A]): SkipList[A] =
throw new Exception("Unsupported operation")
}
Run Code Online (Sandbox Code Playgroud)
好的,这个编译.但与不可变排序集不同,我可以毫不含糊地做
case class Holder(i: Int) extends Ordered[Holder] {
def compare(b: Holder) = i.compare(b.i)
}
def test1(iss: …Run Code Online (Sandbox Code Playgroud) 从(4.4/1)开始
如果"cv2 T"比"cv1 T"更符合cv,则可以将类型"指向cv1 T的指针"的rvalue转换为"指向cv2 T的指针"的rvalue.
我不知道标准定义'更多cv-qualifield'类型的位置,但据我所知,const的声明符比非const更符合cv.
对于以下转换,标准的引用如何适用或如何知道哪一个更少或更多cv-qualifed?
int *const c1 = 0;
int const* c2 = 0;
const int *const c3 = 0;
c1 = c2; // allowed
c1 = c3; // allowed
Run Code Online (Sandbox Code Playgroud)
更新:
c2 = c1;
c2 = c3;
Run Code Online (Sandbox Code Playgroud) 假设我有一个简单的对象支持对System.String的隐式转换
public sealed class CompanyCode
{
public CompanyCode(String value)
{
// Regex validation on value format
_value = value;
}
private readonly String _value;
public override String ToString()
{
return _value;
}
static public implicit operator String(CompanyCode code)
{
if(code == null)
return null;
return code.ToString();
}
}
Run Code Online (Sandbox Code Playgroud)
现在让我们说我的程序的另一部分我用字符串进行比较:
var companyCode = { some company code object }
if (companyCode == "MSFTUKCAMBS")
// do something...
Run Code Online (Sandbox Code Playgroud)
编译器对==运算符做了什么?是否隐式将companyCode转换为字符串并运行System.String ==实现?它是否正在使用System.Object ==实施?或者编译器会抱怨我?(我现在没有编译器来检查这个).
据我所知,我还有其他几种选择.
==(String x)在CompanyCode上实现运算符.IEquatable<String>在CompanyCode上实现接口. …我想完全理解转换,即确保我知道函数调用何时会导致隐式转换,何时会导致编译错误.我已经知道,当且仅当有一种单一的方法将变量转换为以下列表中的两个步骤(按优先级排序)时,才能进行转换:
1. Exact match
2. Promotion
3. Conversion
4. User defined conversion
Run Code Online (Sandbox Code Playgroud)
其中,顺便我的理解是(你可以指正),是推广是原语转化成更大的原始类型,如短整型,浮点翻番,等等; 转换是基元之间的任何转换,不是促销,例如int到char等; 用户定义的转换是使用转换构造函数和转换运算符的类转换.现在,我也知道继承意味着和Is-A关系,这意味着派生类是基类,因此将派生类发送到期望引用基类的函数应该有效.结合上面的两个概念,我们应该得到以下我写的例子,应该工作:
class C {};
class D: public C
{
public:
D(int x){}
};
void f(C& c) {}
f(3);
Run Code Online (Sandbox Code Playgroud)
因为D可以从int转换为,而D是C.但是这个代码没有被编译.这是为什么?如何解决矛盾?你能否解释一下这个问题?谢谢!
检查以下代码:
#include <iostream>
using namespace std;
int& foo() {
static int i = 0;
return i;
}
int main() {
cout << &foo() << endl;
cout << &foo << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
正如你看到的,第一个cout返回值的打印地址foo(),这将是静态变量i里面foo().对于第二个cout我期待那&foo的回报地址foo()功能,如规定在这里:
2)如果操作数是非静态成员的限定名,例如&C :: member,则结果是指向成员函数的prvalue指针或指向类C中类型T的数据成员的指针.注意,&member和C都不是: :member甚至&(C :: member)可用于初始化指向成员的指针.
但令我惊讶的是,这是我的输出:
0x5650dc8dc174
1
Run Code Online (Sandbox Code Playgroud)
第一个是好的,但第二个是1?怎么回事?为了确保我没有弄乱任何东西,我在下面编写了这段代码C:
#include <stdio.h>
int foo() {
}
int main(void) {
printf("%p", &foo);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
以下输出:
0x55732bd426f0 …Run Code Online (Sandbox Code Playgroud) 我刚刚开始使用C ++。
我正在尝试获取字符串“ str”的前三个字符,并将其与已知字符串(例如“ knownString”)进行比较。
为此,我编写了以下代码行:
如果(str.substr(start,3)== knownString)
其中“ start”是我之前声明的整数。但是我一直收到以下警告消息:
警告:隐式转换将签名:“ int”更改为 “ std :: __ cxx11 :: basic_string,** ** std :: allocator> :: size_type”(又名“ unsigned int”)
有谁知道我可以添加什么,或者我错过了此声明以解决此问题?
I decided to test compile a project with -Wsign-conversion enabled, to see what warnings would come up, and came across something that doesn't seem right, where gcc behaves differently than clang. Can someone please tell me which is correct?
I have a function that takes a size_t param:
void func(size_t) {}
Run Code Online (Sandbox Code Playgroud)
some other struct
struct Test {};
Run Code Online (Sandbox Code Playgroud)
and calling code
int i = some_initialiser();
func(sizeof(Test) + static_cast<size_t>(i));
Run Code Online (Sandbox Code Playgroud)
So from my understanding, sizeof returns size_t, and arithmetic between two variables …
c++ ×5
scala ×2
asp.net-mvc ×1
c ×1
c# ×1
collections ×1
const ×1
gcc-warning ×1
mutable ×1
qt-creator ×1
qualifiers ×1
reference ×1
set ×1
upcasting ×1
view ×1