3

I'm trying to write some parameter wrapping helper code like below

#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
}

This code works on fixed parameter function or l-value ref of type without namespace, but my compiler rejects attempt to use r-value ref parameter with some namespace. I'm using vc11 ctp version, and the error message is somewhat incomprehensible (see below)

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

It looks like a compiler bug, but I don't have confidence for that. Is this bug? If not, then what should I do to fix the problem?

7
  • 1
    Definitely looks like an error. Bravo for the test case ;) Commented Nov 15, 2012 at 14:31
  • VC has finally implemented variadic templates? Commented Nov 15, 2012 at 14:50
  • 1
    The CTP variadics are buggy as hell. I tried to port code from GCC/Clang, which uses variadics in a number of ways, and filed 11 variadic bugs that night (+3 for decltype). :) Commented Nov 15, 2012 at 17:35
  • The code compiles under g++ 4.7.1 Commented Nov 16, 2012 at 20:47
  • @Xeo : Can you post links to the bug reports somewhere so we can upvote them please? Commented Nov 29, 2012 at 20:29

1 Answer 1

1

It compiles fine with g++ 4.7.2 and clang++ 3.2. It is probably a compiler bug. It seems that vc11 doesn't implement expanding packs properly.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.