Re: [問題] 有人看過C++ template:complete guide嗎

看板C_and_CPP作者 (阿貓)時間12年前 (2011/12/19 06:15), 編輯推噓2(200)
留言2則, 2人參與, 最新討論串2/2 (看更多)
※ 引述《allstarschh (allstars)》之銘言: : 想請問這本書 : C++ Template : The complete guide : 因為c++ advanced的book裡 有蠻推這本書的 : 所以有買來看 : 但是他裡面的範例 : 有些跟我實際跑起來的結果不太一樣 : 我用g++ 4.4.3 : 像前面幾篇有位網友(t..5116)問template argument的問題 : 就是這本裡面的 : section 8.3 : 書上說會有compile error : 但我試是會過的 而且選了比較直接的multi(T) : 我後來也有試了幾個 : 但也是有遇到不一樣的 : 像10.3.2 : 他說用built-in type的話就會compile error了 : 但我試也沒有 : 這本書應該2002 or 2003出的 : 可能template的spec有再更新了一下 : 所以我想請教一下有沒有人看過這本的 : 有比較過書上哪些section是舊的還沒update過的呢 : 謝謝 書上的這兩個例子應該都是沒有問題的 -- Section 8.3 -- $ cat test.cpp template<typename F, typename T> void apply(F f, T t) { f(t); } template<typename T> void multi(T) { } template<typename T> void multi(T*) { } int main() { apply(&multi<int>, 7); return 0; } 這個例子multi<int>會生出兩個function: 1) void multi(int) 2) void multi(int*) 但是&multi<int>無法辨別是要選哪一個function,所以應當為ambiguous, 但用gcc編譯的話會選第一個void multi(int) [F = void (*)(int)], 這個似乎是gcc的bug,在clang 3.0和msvc2010底下編譯上述程式碼都會產生錯誤: $ clang -Wall -Wextra -pedantic test.cpp test.cpp:19:5: error: no matching function for call to 'apply' apply(&multi<int>, 7); ^~~~~ test.cpp:2:6: note: candidate template ignored: couldn't infer template argument 'F' void apply(F f, T t) ^ 1 error generated. Microsoft Visual C++ 2010: error C2914: 'apply' : cannot deduce template argument as function argument is ambiguous error C2784: 'void apply(F,T)' : could not deduce template argument for 'overloaded function type' from 'overloaded function type' -- Section 10.3.2 -- $ cat test.cpp template<typename T> void f() { g(T()); } void g(int) { } int main() { f<int>(); return 0; } 這裡因為int並沒有其type-associated namespace, (c.f. 書中提到的MyInt的associated namespace為global namespace) 所以用argument-dependent lookup應無法找到void g(int)這個function, 不過舊版的gcc卻可以accept以上程式碼,此bug已在4.7.0版本中修掉: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=24163 $ g++-4.7 --version && g++-4.7 -Wall -Wextra -pedantic test.cpp g++-4.7 (GCC) 4.7.0 20111112 (experimental) Copyright (C) 2011 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. test.cpp: In instantiation of ‘void f() [with T = int]’: test.cpp:13:12: required from here test.cpp:4:5: error: ‘g’ was not declared in this scope, and no declarations were found by argument-dependent lookup at the point of instantiation [-fpermissive] test.cpp:7:6: note: ‘void g(int)’ declared here, later in the translation unit 用clang 3.0一樣會產生編譯錯誤: $ clang -Wall -Wextra -pedantic test.cpp test.cpp:4:5: error: call to function 'g' that is neither visible in the template definition nor found by argument-dependent lookup g(T()); ^ test.cpp:13:5: note: in instantiation of function template specialization 'f<int>' requested here f<int>(); ^ test.cpp:7:6: note: 'g' should be declared prior to the call site void g(int) ^ 1 error generated. -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.113.235.102

12/19 08:56, , 1F
謝謝 那我多試幾個compiler看看
12/19 08:56, 1F

12/20 08:59, , 2F
阿貓學長必拜<(__ __)>
12/20 08:59, 2F
文章代碼(AID): #1ExcNy8b (C_and_CPP)
文章代碼(AID): #1ExcNy8b (C_and_CPP)