gpt4 book ai didi

c++ - 传递 0 而不是 1 时 sfinae 模棱两可的调用

转载 作者:行者123 更新时间:2023-11-30 05:47:42 25 4
gpt4 key购买 nike

相关代码:

#include <iostream>
#include <type_traits>

template<template<typename...> class C, typename... T>
struct is_valid_instantiation_impl
{
// Default constructor
template<template<typename...> class D>
static std::true_type test(decltype(D<T...>{ })*, int);
// Copy constructor
template<template<typename...> class D>
static std::true_type test(decltype(D<T...>{ std::declval<const D<T...>&>() })*, long);
// Move constructor
template<template<typename...> class D>
static std::true_type test(decltype(D<T...>{ std::declval<D<T...>&&>() })*, int*);

template<template<typename...> class D>
static std::false_type test(...);

using type = decltype(test<C>(nullptr, 1));
// ^ this one
};

template<template<typename...> class C, typename... T>
struct is_valid_instantiation : is_valid_instantiation_impl<C, T...>::type { };

template<typename>
struct tester;

template<>
struct tester<int>
{
tester(int);
};

int main(int argc, char** argv)
{
std::cout << "instantiable<int>: " << is_valid_instantiation<tester, int>::value << std::endl;
}

此代码编译正常,但是当我用 0 替换 1 时,我得到 (Clang 3.4)

xxx: error: call to 'test' is ambiguous
using type = decltype(test<C>(nullptr, 0));
^~~~~~~
xxx: note: in instantiation of template class 'is_valid_instantiation_impl<tester, int>' requested here
...

xxx: note: candidate function [with D = tester]
static std::true_type test(decltype(D<T...>{ std::declval<const D<T...>&>() })*, long);
^
xxx: note: candidate function [with D = tester]
static std::true_type test(decltype(D<T...>{ std::declval<D<T...>&&>() })*, int*);
^
xxx: note: candidate function [with D = tester]
static std::false_type test(...);
^

为什么这个调用突然有歧义了?据我所知,0 仍然是一个 int(GCC 4.8.2 似乎同意我的观点,但也不会编译它):

xxx: error: call of overloaded ‘test(std::nullptr_t, int)’ is ambiguous

编辑:

请注意,我不是在问如何解决这种歧义,而是在我使用 0 而不是 1 时为什么会出现歧义。参见 this live example显示错误的完整代码。

最佳答案

我真的不知道你的 CD 是什么,但基本上你有这两个重载:

static std::true_type test(X*, long);
static std::true_type test(Y*, int*);

test(nullptr, 0);

nullptr 匹配第一个参数,0 匹配第二个参数。两种重载都是可行的,并且没有一个比另一个更好。因此:

error: call of overloaded ‘test(std::nullptr_t, int)’ is ambiguous

0 可以传递给 int* 的原因是,来自 [conv.ptr]:

A null pointer constant is an integer literal with value zero or a prvalue of type std::nullptr_t. A null pointer constant can be converted to a pointer type; the result is the null pointer value of that type and is distinguishable from every other value of object pointer or function pointer type.

关于c++ - 传递 0 而不是 1 时 sfinae 模棱两可的调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28505090/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com