template <typename CRTP>
struct Pre {
CRTP & operator++();
};
template <typename CRTP>
struct Post {
CRTP operator++(int);
};
struct Derived
: Pre<Derived>
, Post<Derived>
{};
int main() {
Derived d;
d++;
++d;
}
Run Code Online (Sandbox Code Playgroud)
我从GCC得到这些错误:
<source>: In function 'int main()':
<source>:18:10: error: request for member 'operator++' is ambiguous
d++;
^~
<source>:8:14: note: candidates are: CRTP Post<CRTP>::operator++(int) [with CRTP = Derived]
CRTP operator++(int);
^~~~~~~~
<source>:3:16: note: CRTP& Pre<CRTP>::operator++() [with CRTP = Derived]
CRTP & operator++();
^~~~~~~~
<source>:19:11: error: request for member …Run Code Online (Sandbox Code Playgroud) 为什么doSomething编译器会先调用前两个,但是在列表中使用两个元素会导致调用不明确?
#include <vector>
#include <string>
void doSomething(const std::vector<std::string>& data) {}
void doSomething(const std::vector<int>& data) {}
int main(int argc, char *argv[])
{
doSomething({"hello"}); // OK
doSomething({"hello", "stack", "overflow"}); // OK
doSomething({"hello", "stack"}); // C2668 'doSomething': ambiguous call
return 0;
}
Run Code Online (Sandbox Code Playgroud) 考虑一下这段C++ 11代码:
#include <iostream>
#include <cstddef>
template<typename T> void f(T, const char*) //#1
{
std::cout << "f(T, const char*)\n";
}
template<std::size_t N> void f(int, const char(&)[N]) //#2
{
std::cout << "f(int, const char (&)[N])\n";
}
int main()
{
f(7, "ab");
}
Run Code Online (Sandbox Code Playgroud)
好吧,那么...选择哪个超载?在使用编译器输出溢出bean之前,让我们试着解释一下.
(所有对部分的引用都是针对C++ 11,ISO/IEC 14882:2011的最终标准文档.)
T从#1推断出int,N从#2推断出来3,两个专业都是候选者,两者都是可行的,到目前为止都很好.哪一个最好?
首先,考虑将函数参数与函数参数匹配所需的隐式转换.对于第一个参数,在任何一种情况下(身份转换)都不需要转换,int无处不在,所以这两个函数同样好.对于第二个,参数类型是const char[3],并且两个转换是:
[13.3.3.1.1]; 在根据转换序列进行比较时会忽略此转换类别[13.3.3.2],因此这与此目的的身份转换 …I am using a LINQ query on a DbSet<T>:
await _dbContext.Users.AnyAsync(u => u.Name == name);
Run Code Online (Sandbox Code Playgroud)
However, the compiler outputs the following error:
Error CS0121: The call is ambiguous between the following methods or properties:
'System.Linq.AsyncEnumerable.AnyAsync<TSource>(System.Collections.Generic.IAsyncEnumerable<TSource>, System.Func<TSource, bool>)' and
'Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.AnyAsync<TSource>(System.Linq.IQueryable<TSource>, System.Linq.Expressions.Expression<System.Func<TSource, bool>>)'
Run Code Online (Sandbox Code Playgroud)
A similar problem also occurs with other LINQ extension methods, like .Where().
I am using EF.Core 3.1 and have the System.Linq.Async package installed. How do I fix this issue?
c# linq ambiguous entity-framework-core entity-framework-core-3.1
我有一个类似的扩展方法
public static class Extension
{
public static string GetTLD(this string str)
{
var host = new System.Uri(str).Host;
int index = host.LastIndexOf('.'), last = 3;
while (index >= last - 3)
{
last = index;
index = host.LastIndexOf('.', last - 1);
}
var domain = host.Substring(index + 1);
return domain;
}
}
Run Code Online (Sandbox Code Playgroud)
我称之为
string domain = "." + _url.GetTLD();
Run Code Online (Sandbox Code Playgroud)
我在构建和清理构建时没有错误.
但我得到compilation error运行时错误说
以下方法或属性之间的调用不明确:'myignOU.Extension.GetTLD(string)'和'myIGNOU.Extension.GetTLD(string)'
我发誓我没有把这个扩展方法放在项目中的任何其他位置.为什么我只在运行时收到此错误..?
但是,如果我删除此方法,那么我在构建时遇到错误,而不是在运行时.没有这种方法的代码,一切正常.
在下面的代码段,在函数调用f(1),1是文字型的int并在第一功能void f(double d)参数类型是double和第二函数void f(short int i)参数类型是短整型.
这1是一个int不是double类型的类型,那么为什么编译器会产生模糊错误?
#include <iostream>
using namespace std;
void f(double d) // First function
{
cout<<d<<endl;
}
void f(short int i) // Second function
{
cout<<i<<endl;
}
int main()
{
f(1); // 1 is a literal of type int
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我真的不明白这一点,我认为编译器首先执行大括号中的内容然后将结果赋予最合适的函数.这看起来它给函数一个初始化列表来处理它...
#include <string>
#include <vector>
using namespace std;
void func(vector<string> v) { }
void func(vector<wstring> v) { }
int main() {
func({"apple", "banana"});
}
Run Code Online (Sandbox Code Playgroud)
错误:
<stdin>: In function 'int main()':
<stdin>:11:27: error: call of overloaded 'func(<brace-enclosed initializer list>)' is ambiguous
<stdin>:11:27: note: candidates are:
<stdin>:6:6: note: void func(std::vector<std::basic_string<char> >)
<stdin>:8:6: note: void func(std::vector<std::basic_string<wchar_t> >)
Run Code Online (Sandbox Code Playgroud)
为什么不func(vector<string> v)调用我的超载,我可以这样做吗?
operator bool打破了operator<以下示例中的使用.任何人都可以解释为什么bool仅仅是在为相关if (a < 0)表达的具体操作者,是否有解决方法吗?
struct Foo {
Foo() {}
Foo(int x) {}
operator bool() const { return false; }
friend bool operator<(const Foo& a, const Foo& b) {
return true;
}
};
int main() {
Foo a, b;
if (a < 0) {
a = 0;
}
return 1;
}
Run Code Online (Sandbox Code Playgroud)
当我编译时,我得到:
g++ foo.cpp
foo.cpp: In function 'int main()':
foo.cpp:18:11: error: ambiguous overload for 'operator<' (operand types are 'Foo' and 'int')
if (a …Run Code Online (Sandbox Code Playgroud) main方法尝试访问var,但会导致调用模糊.为什么?无论如何,无法从静态上下文访问(可见?)Base1中的实例变量var.
class Base1 {
int var;
}
interface Base2 {
public static final int var = 0;
}
class Test extends Base1 implements Base2 {
public static void main(String args[]) {
System.out.println("var:" + var);
}
}
Run Code Online (Sandbox Code Playgroud) 在通用函数中,我使用以下习语,
template<class It1, class It2>
void do_something(It1 first, It1 second, It2 d_first){
... other stuff here...
using std::copy;
copy(first, second, d_first);
}
Run Code Online (Sandbox Code Playgroud)
do_something是一个通用函数,不应该知道任何其他任何库(可能除外std::).
现在假设我的命名空间中有几个迭代器N.
namespace N{
struct itA{using trait = void;};
struct itB{using trait = void;};
struct itC{using trait = void;};
}
Run Code Online (Sandbox Code Playgroud)
我想在这个命名空间中重载这些迭代器的副本.我当然会这样做:
namespace N{
template<class SomeN1, class SomeN2>
SomeN2 copy(SomeN1 first, SomeN1 last, SomeN2 d_first){
std::cout << "here" << std::endl;
}
}
Run Code Online (Sandbox Code Playgroud)
然而,当我打电话do_something用N::A,N::B或N::C争论,我得到"暧昧通话复制"即使这些都在同一个命名空间N::copy.
有没有办法 …