2012-11-15 58 views
3

我想寫像下面編譯錯誤

#include <type_traits> 
#include <string> 

struct test{}; 

namespace ns { 
    struct test{}; 
} 

template<typename T> 
struct arg_wrapper; 

template<> 
struct arg_wrapper<test> 
{ 
    arg_wrapper(test&) {} 
}; 

template<> 
struct arg_wrapper<ns::test> 
{ 
    arg_wrapper(ns::test&) {} 
}; 

template<> 
struct arg_wrapper<std::string> 
{ 
    arg_wrapper(const std::string& value) {} 
}; 

template<typename... Args> 
void callee(const Args&... args) 
{ 
} 

template<typename... Args> 
void wrapper_variadic(Args&&... args) 
{ 
    callee(arg_wrapper<typename std::decay<Args>::type>(args)...); 
} 

template<typename Args> 
void wrapper_fixed(Args&& args) 
{ 
    callee(arg_wrapper<typename std::decay<Args>::type>(args)); 
} 

int main() 
{ 
    std::string a("test"); 
    wrapper_variadic(a); // works well 
    wrapper_variadic(std::string("test")); // compile error 
    wrapper_variadic(test()); // works well 
    wrapper_variadic(ns::test()); // compile error 
    wrapper_fixed(std::string("test")); // works well 
    wrapper_fixed(test()); // works well 
    wrapper_fixed(ns::test()); // works well 
} 

此代碼對類型的固定參數的函數或L值裁判沒有命名空間的一些參數,包裝輔助代碼,但我的編譯器拒絕嘗試在某些名稱空間中使用r值ref參數。我使用VC11 CTP版本,並且錯誤信息是有點難以理解(見下文)

1>d:\work\toy\vs2012test\vs2012test\source.cpp(39): error C2065: 'basic_string<char,std::char_traits<char>,std::allocator<char> >' : undeclared identifier 
1>   d:\work\toy\vs2012test\vs2012test\source.cpp(52) : see reference to function template instantiation 'void wrapper_variadic<std::basic_string<char,std::char_traits<char>,std::allocator<char>>>(std::basic_string<char,std::char_traits<char>,std::allocator<char>> &&)' being compiled 
1>d:\work\toy\vs2012test\vs2012test\source.cpp(39): error C2923: 'std::decay' : 'basic_string<char,std::char_traits<char>,std::allocator<char> >' is not a valid template type argument for parameter '_Ty' 
1>d:\work\toy\vs2012test\vs2012test\source.cpp(39): error C2955: 'std::decay' : use of class template requires template argument list 
1>   c:\program files (x86)\microsoft visual studio 11.0\vc\include\type_traits(1620) : see declaration of 'std::decay' 
1>d:\work\toy\vs2012test\vs2012test\source.cpp(39): error C2440: '<function-style-cast>' : cannot convert from 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>' to 'arg_wrapper<_If<std::is_array<remove_reference<_Ty>::type>::value,remove_extent<remove_reference<_Ty>::type>::type*,_If<std::is_function<remove_reference<_Ty>::type>::value,add_pointer<remove_reference<_Ty>::type>::type,remove_cv<remove_reference<_Ty>::type>::type>::type>::type>' 
1>   Source or target has incomplete type 
1>d:\work\toy\vs2012test\vs2012test\source.cpp(39): error C2440: '<function-style-cast>' : cannot convert from 'ns::test' to 'arg_wrapper<test>' 
1>   No constructor could take the source type, or constructor overload resolution was ambiguous 
1>   d:\work\toy\vs2012test\vs2012test\source.cpp(54) : see reference to function template instantiation 'void wrapper_variadic<ns::test>(ns::test &&)' being compiled 

它看起來像一個編譯器錯誤,但我不具備這種信心。這個錯誤?如果不是,那我該怎麼辦才能解決這個問題?

+1

絕對看起來像一個錯誤。 Bravo的測試用例;) –

+0

VC終於實現了可變模板? – mfontanini

+1

CTP variadics是地獄的車。我嘗試從GCC/Clang移植代碼,該代碼以多種方式使用可變參數,並在當晚提交了11個可變參數錯誤(+ decltype)。 :) – Xeo

回答

1

它用g ++ 4.7.2和clang ++ 3.2編譯得很好。這可能是一個編譯器錯誤。看來,vc11並沒有正確實施擴展包。