为什么这段代码
struct ThingComparator
{
...
}
static void Blah (set <CString> &things)
{
...
}
...
set<CString, ThingComparator>things;
Blah (things);
无法使用以下错误进行编译(Visual Studio 2010):
error C2664: 'Blah' : cannot convert parameter 1 from 'std::set<_Kty,_Pr>' to 'std::set<_Kty> &'
我的C ++知识显然是有限的,但我希望听到一个小号宣布他的可信赖的骏马上的多态骑士,但我所听到的只是一个马屁和一个悲伤的长号: - (
答案 0 :(得分:9)
3 lazyFactory) at Microsoft.VisualStudio.Composition.RuntimeExportProviderFactory.RuntimeExportProvider.GetValueForImportSite(RuntimePartLifecycleTracker importingPartTracker, RuntimeImport import) at Microsoft.VisualStudio.Composition.RuntimeExportProviderFactory.RuntimeExportProvider.RuntimePartLifecycleTracker.<CreateValue>b__10_0(RuntimeImport import) at System.Linq.Enumerable.WhereSelectEnumerableIterator
被声明为as follows:
1..ctor(IEnumerable
因此1 source) at Microsoft.VisualStudio.Composition.RuntimeExportProviderFactory.RuntimeExportProvider.RuntimePartLifecycleTracker.CreateValue() at Microsoft.VisualStudio.Composition.ExportProvider.PartLifecycleTracker.Create() at Microsoft.VisualStudio.Composition.ExportProvider.PartLifecycleTracker.MoveNext(PartLifecycleState nextState) at Microsoft.VisualStudio.Composition.ExportProvider.PartLifecycleTracker.MoveToState(PartLifecycleState requiredState) at Microsoft.VisualStudio.Composition.ExportProvider.PartLifecycleTracker.GetValueReadyToExpose() at Microsoft.VisualStudio.Composition.RuntimeExportProviderFactory.RuntimeExportProvider.<>c__DisplayClass15_0.<GetExportedValueHelper>b__0() at Microsoft.VisualStudio.Composition.DelegateServices.<>c__DisplayClass2_0
实际上意味着1.CreateValue() at System.Lazy
,1.get_Value() at Microsoft.VisualStudio.Text.Utilities.GuardedOperations.InvokeMatchingFactories[TExtensionInstance,TExtensionFactory,TMetadataView](IEnumerable
不是std::set
。写下以下内容:
template<
class Key,
class Compare = std::less<Key>,
class Allocator = std::allocator<Key>
> class set;
答案 1 :(得分:1)
所涉及的多态性不是运行时多态性,在您的情况下是必需的。该函数应该是一个模板或std::set<CString, std::function<bool(const CString&, const CString&)>>
来显式调用比较器上的运行时多态性。