c++ 使用一個使用成員類型別名的建構函式來引用類模板引數
顯然這應該是有效的:
template<class T> struct C { using value_type = T; C(value_type); }; C c(1); // C<int>
就這樣(見ofollow,noindex" target="_blank">[over.match.class.deduct]/3 中的B示例):
template<class T> struct D { template<class> using meow_t = T; template<class U> D(U, meow_t<U>); }; D d(1, 'c'); // D<char>
請注意,由於引數是未推斷的上下文,因此看似相當的顯式指南將不起作用:
template<class T> C(typename C<T>::value_type) -> C<T>;
儘管至少第一個程式碼片段的工作肯定是可取的,但我還沒有找到實際上使其在當前工作草案中工作的措辭.有誰知道它在哪裡?
這不是一個嚴格的答案,因為我不認為這樣的措辭實際上是存在的.這更多的是與這個問題相關的資訊.
這是Core Issue 2 .在Oulu和Issaquah關於這個功能的討論清楚地表明,目的是通過typedefs檢視是有效的,但是沒有新增任何措辭來表明這是如何運作的 – 它只是… .這個措辭表明,扣除指南為:
template<class T> struct C { using value_type = T; C(value_type); };
將會:
template <class T> C<T> foo(typename C<T>::value_type );
這將是一個未被推斷的情況並失敗,但[thread.lock.guard] 沒有明確的扣除指南.
[over.match.best] 中的示例顯然是為了表明typedefs應該可以工作,儘管該示例中的示例實際上不使用#1作為扣除指南:
06002
程式碼日誌版權宣告:
翻譯自:http://stackoverflow.com/questions/43530295/deducing-class-template-arguments-with-a-constructor-that-uses-a-member-type-ali