相关疑难解决方法(0)

类模板特化部分排序和函数合成

选择哪个类模板特化的首选规则包括将特化重写为函数模板,并通过函数模板[temp.class.order]的排序规则确定哪个函数模板更加专业化.考虑这个例子,然后:

#include <iostream>

template <class T> struct voider { using type = void; };
template <class T> using void_t = typename voider<T>::type;

template <class T, class U> struct A { };

template <class T> int foo(A<T, void_t<T>> ) { return 1; }
template <class T> int foo(A<T*, void> )     { return 2; }

int main() {
    std::cout << foo(A<int*, void>{});
}
Run Code Online (Sandbox Code Playgroud)

gcc和clang都打印2在这里.这是有道理的一些前面的例子-推导对非推测的情况下(voidvoid_t<T>)只是忽略,所以推断<T, void_t<T>>反对<X*, void>成功,但推断<T*, void>针对<Y, void_t<Y>>在两个参数失败.精细. …

c++ templates partial-ordering language-lawyer

43
推荐指数
1
解决办法
1416
查看次数

这种模板函数重载的情况使我无法理解

#include <iostream>

template<typename T>
struct identity
{
    typedef T type;
};

template<typename T> void bar(T) { std::cout << "a" << std::endl; }
template<typename T> void bar(typename identity<T>::type) { std::cout << "b" << std::endl; }

int main ()
{
    bar(5); // prints "a" because of template deduction rules
    bar<int>(5); // prints "b" because of ...?

    return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)

我预计bar<int>(5)至少会产生歧义.这里涉及到关于模板函数重载决策的疯狂规则?

c++ templates language-lawyer overload-resolution

30
推荐指数
1
解决办法
442
查看次数

模板偏序 - 为什么部分演绎在这里成功

考虑以下简单(在模板问题的范围内)示例:

#include <iostream>

template <typename T>
struct identity;

template <>
struct identity<int> {
    using type = int;
};

template<typename T> void bar(T, T ) { std::cout << "a\n"; }
template<typename T> void bar(T, typename identity<T>::type) { std::cout << "b\n"; }

int main ()
{
    bar(0, 0);
}
Run Code Online (Sandbox Code Playgroud)

clang和gcc都在那里打印"a".根据[temp.deduct.partial]和[temp.func.order]中的规则,为了确定部分排序,我们需要合成一些独特的类型.所以我们有两次尝试扣除:

+---+-------------------------------+-------------------------------------------+
|   | Parameters                    | Arguments                                 |
+---+-------------------------------+-------------------------------------------+
| a | T, typename identity<T>::type | UniqueA, UniqueA                          |
| b | T, T                          | UniqueB, typename identity<UniqueB>::type |
+---+-------------------------------+-------------------------------------------+
Run Code Online (Sandbox Code Playgroud)

根据Richard Corden的回答 …

c++ templates partial-ordering language-lawyer overload-resolution

20
推荐指数
2
解决办法
1166
查看次数

使用非推导上下文的部分特化排序

根据[temp.class.order]§14.5.5.2,t在此示例中选择部分特化:

template< typename >
struct s { typedef void v, w; };

template< typename, typename = void >
struct t {};

template< typename c >
struct t< c, typename c::v > {};

template< typename c >
struct t< s< c >, typename s< c >::w > {};

t< s< int > > q;
Run Code Online (Sandbox Code Playgroud)

等效f于此示例中的重载选择:

template< typename >
struct s { typedef void v, w; };

template< typename, typename = void >
struct t {};

template< typename …
Run Code Online (Sandbox Code Playgroud)

c++ templates partial-specialization partial-ordering language-lawyer

8
推荐指数
2
解决办法
525
查看次数