รหัสต่อไปนี้ คอมไพล์สำเร็จใน g++ 7.2.0 (แฟล็กการคอมไพล์คือ -std=c++14 -Wall -Wextra -Werror -pedantic-errors
) แต่ไม่สามารถคอมไพล์ใน clang++ 5.0.0 (ด้วยแฟล็กเดียวกัน -std=c++14 -Wall -Wextra -Werror -pedantic-errors
) และ vc++ 15.4 (แฟล็กการคอมไพล์คือ /EHsc /Za /std:c++14 /permissive-
):
template <template <typename...> class Functor, typename... FixedArguments>
struct apply
{
template <typename... FreeArguments>
using type = Functor<FixedArguments..., FreeArguments...>;
};
template <typename, typename>
struct Bar{};
template <template <typename...> class>
struct Foo{};
int main()
{
(void)Foo<apply<Bar, int, char>::type>{};
}
พฤติกรรมของคอมไพเลอร์ใดที่เป็นไปตามมาตรฐาน เทมเพลต apply
ดังกล่าวสามารถเปลี่ยนให้คอมไพล์บน clang++ ได้อย่างไร
ข้อความแสดงข้อผิดพลาด เสียงดังกราว++:
5 : <source>:5:15: error: too many template arguments for class template 'Bar' using type = Functor<FixedArguments..., FreeArguments...>; ^ ~~~~~~~~~~~~~~~~~ 16 : <source>:16:15: note: in instantiation of template class 'apply<Bar, int, char>' requested here (void)Foo<apply<Bar, int, char>::type>{}; ^ 9 : <source>:9:8: note: template is declared here struct Bar{};
ข้อความแสดงข้อผิดพลาด vc++:
5 : <source>(5): error C2977: 'Bar': too many template arguments 9 : <source>(9): note: see declaration of 'Bar' 16 : <source>(16): note: see reference to class template instantiation 'apply<Bar,int,char>' being compiled
typename...
ถึงBar
? - person songyuanyao   schedule 22.11.2017Bar
และFoo
เป็นคลาสตัวอย่างที่นี่เพื่อแสดงปัญหา ลักษณะของพวกเขาได้รับจากภายนอก ในความเป็นจริง MWE ที่เล็กที่สุดแสดงให้เห็นถึงปัญหาในโค้ดเทมเพลตที่ซับซ้อนบางส่วน - person Constructor   schedule 22.11.2017apply<Bar, int, char>::type
นั้นBar<int, char>
จะมีความเชี่ยวชาญเฉพาะทางอย่างเต็มที่ ดังนั้นจึงไม่มีทางที่จะสามารถป้อนประเภทเพิ่มเติมไปยังFunctor
ในภายหลังได้ - person piwi   schedule 22.11.2017