Note: pls don't be confused with other Stackoverflow questions that have the same title/errocode -- they are mostly due to missing #include <string> but it's not the issue here.
I'm reading Pretty-print C++ STL containers question and downloaed Kerrek's code from his GitHub Project cxx-prettyprint, when I try compile it (my environment is Windows 7, Visual Studio 2013, compiled project as a Console Application "Use Multi-Byte Character Set"), hit a template deduction error.
It's a bit weird as suggested by Mike Kinghan in the comment, it does compile compiles and runs fine with the current MSVC++ : see it online.
I'm confused, there must be something wrong with my configuration. I created a blank new Windows Console Application, and added Kerrek's code in, focus on a vector of string test cast, removed other scenarios such as set/map etc:
int main(int argc, char* argv[])
{
std::vector<std::string> v;
v.push_back("hello, world");
v.push_back("2nd line");
v.push_back("last line");
std::cout << v;
}
, now hit a error:
Error 1 error C2679: binary '<<' : no operator found which takes a
right-hand operand of type
'std::vector>' (or there is no
acceptable conversion)
If I explicitly call the pretty_print::operator <<,
pretty_print::operator<<(std::cout, v);
, hit another template deduction error:
Error 1 error C2784: 'std::basic_ostream<_Elem,_Traits>
&pretty_print::operator <<(std::basic_ostream<_Elem,_Traits> &,const
pretty_print::print_container_helper
&)' : could not deduce template argument for 'const
pretty_print::print_container_helper
&' from 'std::vector>'
It only compiles if the print_container_helper is explictily created:
pretty_print::print_container_helper<std::vector<std::string>, char, ::std::char_traits<char>, pretty_print::delimiters<std::vector<std::string>, char>>
vh(v);
pretty_print::operator<<(std::cout, vh);
The full code is here: http://rextester.com/RJX76690
Related
Update: using catch 1.9.7 solved this problem.
I am just getting started with using catch and have been unable to get even a simple test project to compile. I am using Visual Studio 2008 and have catch.hpp v1.10.0 (single file version).
I created a simple test project following the catch tutorial. main.cpp is the only file and the code consists of:
#define CATCH_CONFIG_MAIN
#include "catch.hpp"
TEST_CASE("test case 1")
{
REQUIRE(1==2);
}
I get the following errors when I compile:
c:\utilities\catch\catchtest\catchtest\catch.hpp(1333) : warning C4181: qualifier applied to reference type; ignored
c:\utilities\catch\catchtest\catchtest\catch.hpp(1838) : see reference to class template instantiation 'Catch::Internal::Evaluator<T1,T2,Op>' being compiled
with
[
T1=const int &,
T2=const int &,
Op=IsEqualTo
]
c:\utilities\catch\catchtest\catchtest\catch.hpp(1836) : while compiling class template member function 'void Catch::BinaryExpression<LhsT,Op,RhsT>::endExpression(void) const'
with
[
LhsT=const int &,
Op=IsEqualTo,
RhsT=const int &
]
c:\utilities\catch\catchtest\catchtest\main.cpp(8) : see reference to class template instantiation 'Catch::BinaryExpression<LhsT,Op,RhsT>' being compiled
with
[
LhsT=const int &,
Op=IsEqualTo,
RhsT=const int &
]
c:\utilities\catch\catchtest\catchtest\catch.hpp(1333) : error C2529: 'lhs' : reference to reference is illegal
c:\utilities\catch\catchtest\catchtest\catch.hpp(1333) : warning C4181: qualifier applied to reference type; ignored
c:\utilities\catch\catchtest\catchtest\catch.hpp(1333) : error C2529: 'rhs' : reference to reference is illegal
c:\utilities\catch\catchtest\catchtest\catch.hpp(1838) : error C2664: 'Catch::Internal::Evaluator<T1,T2,Op>::evaluate' : cannot convert parameter 1 from 'const int' to 'const int &(&)'
with
[
T1=const int &,
T2=const int &,
Op=IsEqualTo
]
c:\utilities\catch\catchtest\catchtest\catch.hpp(1839) : error C2228: left of '.endExpression' must have class/struct/union
Any assistance would be much appreciated. I haven't been able to find anything in the catch documentation or online and am eager to get started with it.
The latest master of Catch is for modern C++ compiler and the Visual Studio 2008 compiler is pretty old now. There's some switches you can use to downgrade some of the features:
https://github.com/philsquared/Catch/blob/master/docs/configuration.md
Try it with 'CATCH_CONFIG_NO_CPP11' defined before #include'ing catch.
For future reference: support for MSVC 9 was broken by this commit and reverting b6e7c9bd7a160c07c5de894292022687895c17a3 (done on top of it) and then this one is sufficient to fix the problem.
Consider this code snippet:
void Foo(std::string str1, std::string str2) {}
template<typename... Types>
void Bar()
{
Foo(Types{}...); // wont compile
}
Bar<std::string, std::string>();
What I want to do here is to default construct two std::string objects inside the Bar method and pass them to Foo. However my vain attempts (one of them being in the snippet) wont compile so I am wondering whether this is even possible.
I compiled with VC 2013, which throws compiler errors at me. As stated in the comments, other compilers can handle it. Can anyone tell whether the above snippet is standard conform?
It's a problem in the MSVC variadic template expansion process; when it unpacks the list of types it fails to recognise them as suitable for a constructor call. As a workaround, you can perform a type transformation to force the compiler to recognise them:
template<typename T> using identity_t = T; // NEW CODE
void Foo(int, int);
template<typename... Types>
void Bar()
{
Foo(identity_t<Types>{}...); // use identity type transformation
}
int main() {
Bar<int, int>();
}
I haven't managed to find an issue number yet.
This crashes the VC 2013 compiler for me. The errors seem to indicate that it has some problems parsing the code. So when the compiler crashes it must be a compiler bug.
1>main.cpp(23): error C2144: syntax error : 'std::string' should be preceded by ')'
1> main.cpp(28) : see reference to function template instantiation 'void Bar<std::string,std::string>(void)' being compiled
1>main.cpp(23): error C2660: 'Foo' : function does not take 0 arguments
1>main.cpp(23): error C2143: syntax error : missing ';' before '{'
1>main.cpp(23): error C2143: syntax error : missing ';' before ','
1>c1xx : fatal error C1063: INTERNAL COMPILER ERROR
1> Please choose the Technical Support command on the Visual C++
1> Help menu, or open the Technical Support help file for more information
1>cl : Command line warning D9028: minimal rebuild failure, reverting to normal build
1>
1>Build FAILED.
I'm using Visual Studio 2005 and Boost 1.37. I also tested this same code on Visual Studio 2012 Express Desktop and Boost 1.50 without success.
I want to use a Boost.Lambda by accessing a custom subscript operator on my type. It also happens when using with std::array, so I'll illustrate the problem with the std::array type:
#include <vector>
#include <array>
#include <algorithm>
int main() {
std::vector<std::array<int, 3>> arrays;
arrays.push_back(make_array(1, 2, 3));
arrays.push_back(make_array(5, 5, 6));
std::for_each(arrays.begin(), arrays.end(), (_1[0])); //This line fails!
return 0;
}
The errors are:
error C2664: 'boost::lambda::detail::unspecified::unspecified(const boost::lambda::detail::unspecified &)' : cannot convert parameter 1 from 'int' to 'const boost::lambda::detail::unspecified &'
Reason: cannot convert from 'int' to 'const boost::lambda::detail::unspecified'
No constructor could take the source type, or constructor overload resolution was ambiguous
... ad infinitum...
I found this page: Extending return type deduction system
But I couldn't successfully implement it.
Does anyone know what can be done here?
I'm trying to fill std::map with std::transform. Next code compiles without error:
std::set<std::wstring> in; // "in" is filled with data
std::map<std::wstring, unsigned> out;
std::transform(in.begin(), in.end()
, boost::counting_iterator<unsigned>(0)
, std::inserter(out, out.end())
, [] (std::wstring _str, unsigned _val) { return std::make_pair(_str, _val); }
);
But If I replace string
, [] (std::wstring _str, unsigned _val) { return std::make_pair(_str, _val); }
with
, std::make_pair<std::wstring, unsigned>
or
, std::ptr_fun(std::make_pair<std::wstring, unsigned>)
I get errors:
foo.cpp(327): error C2784: '_OutTy *std::transform(_InIt1,_InIt1,_InTy (&)[_InSize],_OutTy (&)[_OutSize],_Fn2)' : could not deduce template argument for '_InTy (&)[_InSize]' from 'boost::counting_iterator<Incrementable>'
with
[
Incrementable=unsigned int
]
C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\algorithm(1293) : see declaration of 'std::transform'
foo.cpp(327): error C2784: '_OutTy *std::transform(_InIt1,_InIt1,_InIt2,_OutTy (&)[_OutSize],_Fn2)' : could not deduce template argument for '_OutTy (&)[_OutSize]' from 'std::insert_iterator<_Container>'
with
[
_Container=std::map<std::wstring,unsigned int>
]
C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\algorithm(1279) : see declaration of 'std::transform'
foo.cpp(327): error C2784: '_OutIt std::transform(_InIt1,_InIt1,_InTy (&)[_InSize],_OutIt,_Fn2)' : could not deduce template argument for '_InTy (&)[_InSize]' from 'boost::counting_iterator<Incrementable>'
with
[
Incrementable=unsigned int
]
C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\algorithm(1267) : see declaration of 'std::transform'
foo.cpp(327): error C2914: 'std::transform' : cannot deduce template argument as function argument is ambiguous
and so on...
Please explain what is the problem is with compilation?
UPDATE: Thanks for answers. I realized, that it is MSVC2010 bug. By the way the line
&std::make_pair<const std::wstring&, const unsigned&>
causes the same error
This is a bug in the Visual C++ library implementation. The following program demonstrates the issue:
#include <utility>
int main()
{
&std::make_pair<int, int>;
};
This program yields the error:
error C2568: 'identifier' : unable to resolve function overload
In the C++ language specification, make_pair is not an overloaded function. The Visual C++ 2010 library implementation includes four overloads taking various combinations of lvalue and rvalue references.
While an implementation of the C++ Standard Library is allowed to add overloads for member functions, it isn't allowed to add overloads for nonmember functions, thus this is a bug.
The bug was reported and will be fixed in the next version of Visual C++. However, as STL notes in the resolution to that bug, you'll need to make the template arguments to std::make_pair lvalue references:
&std::make_pair<const std::wstring&, const unsigned&>
g++ 4.4.5 compiles it without errors. Seems to be a Visual Studio 10 defect.
Consider the following program:
#include <iostream>
#include "boost/filesystem.hpp"
int main()
{
boost::filesystem::directory_entry d("test.txt");
boost::filesystem::directory_entry e("test.txt");
if (d == e) { // <---- error C2784
std::cout << "equal" << std::endl;
}
return 0;
}
This fails to compile (Visual Studio 2005, Windows XP SP3) with 17 variations of this error:
error C2784: 'bool std::operator ==(const std::stack<_Ty,_Container> &,
const std::stack<_Ty,_Container> &)' :
could not deduce template argument for
'const std::stack<_Ty,_Container> &' from
'boost::filesystem::directory_entry'
According to the documentation (I am still using Boost 1.45), there are comparison operators defined for directory_entry, but neither me nor the compiler can find them (I checked the headers manually). Am I overlooking something? Could it be that I made a mistake when building boost, maybe by setting some option that disables these operators? Is the documentation wrong? Can anyone explain?
If you were unable to locate the operator in the header file, then maybe you have a different version of the library? In Boost 1.45, the operator is located in operations.hpp.
Ok, apparently this is only supported in the new Version of the library. Defining BOOST_FILESYSTEM_VERSION as 3 at the start of the program fixed the problem.