The following code gives compilation errors on both MSVC++ 2012 CTP (with C+11 support) and Intel C++ XE 13.0:
template <typename F, typename... Args>
void apply(F f, std::tuple<Args...>& args) {
// doesn't do much yet
}
bool f1(char c) {
return c == 'c';
}
int main(int argc, char* argv[]) {
auto t = std::make_tuple('c');
apply(f1, t);
return 0;
}
VS2012 error is:
error C2243: 'type cast' :
conversion from 'std::tuple<char,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil> *'
to 'std::tuple<std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil> &' exists,
but is inaccessible
Error on Intel C++ XE 13.0 is:
error : no instance of function template "apply" matches the argument list
What am I missing? Are there really errors here, or do I just have two bad compilers?
UPDATE: Same (or similar) results when I use boost::tuple instead of std::tuple on both compilers.
ADDENDUM: Thanks for all the cross-checks in the comments. I've sent bug reports to both of these fine corporations.
That is likely a VC11 bug in the implementation of the Standard Library. Although the CTP supports variadic templates, to the best of my knowledge the Standard Library was not rewritten to use them and adopts some machinery to simulate variadic templates. This is likely at the origin of your problem.
Related
I'm currently trying to deduce a std::tuple type from several std::vectors that are passed as parameters. My code works fine using gcc, but the compilation fails with Visual Studio Professional 2019 with the message "fatal error C1202: recursive type or function dependency context too complex".
It has been mentioned for my previous post (C++ "fatal error C1202: recursive type or function dependency context too complex" in visual studio, but gcc compiles) that the problem is caused by template recursion as explained in C++ template compilation error - recursive type or function dependency. However, I don't see where the recursion occurs.
So my questions are:
Why is there an (infinite) recursion?
How can it be resolved?
Here is the code (I'm bound to C++11):
#include <tuple>
#include <vector>
template <typename TT,typename Add>
auto addTupBase(TT t,std::vector<Add> a) ->decltype (std::tuple_cat(t,std::make_tuple(a[0])))
{
return std::tuple_cat(t,std::make_tuple(a[0])) ;
}
template <typename TT,typename Add,typename... Args>
auto addTupBase(TT t,std::vector<Add> a,Args... args)-> decltype(addTupBase(addTupBase(t,a),args...))
{
return addTupBase(addTupBase(t,a),args...);
}
template <typename T,typename... Args>
auto addTup(std::vector<T> in,Args... args) ->decltype(addTupBase(std::make_tuple(in[0]),args...))
{
return addTupBase(std::make_tuple(in[0]),args...);
}
int main()
{
using TupleType = decltype(addTup(std::vector<char>{2},std::vector<int>{5},std::vector<double>{32423}));
TupleType t;
std::get<2>(t) = 342.2;
return 0;
}
MSVC has some conformance issues when running in /permissive mode and "The Microsoft C++ compiler doesn't currently support binding nondependent names when initially parsing a template. This doesn't conform to section 14.6.3 of the C++ 11 ISO specification. This can cause overloads declared after the template (but before the template is instantiated) to be seen.".
If you instead use /permissive- "The compiler [...] implements more of the requirements for two-phase name look-up" and will compile your program as-is.
Also note that you aren't actually using C++11. MSVC 19.24 supports C++14 and up.
I am trying to port some C++17 code I made on ubuntu (gnu++11)
typedef boost::variant<int, float, std::string > Variant;
using Func = std::function<std::vector<unsigned char>(std::vector<Variant>)>;
void addexecutorfunc( Func callback, const auto&...args )
{
std::vector<Variant> vec = {args...};
executor.add(vec, std::move(callback));
}
this code compiles and works fine on ubuntu, but when trying to compile on windows with visual studio 2017(v141) [ISO C++ Latest Draft Standard(/std:c++latest)], then I get following:
error C3533: a parameter cannot have a type that contains 'auto'
I think perhaps it has to do with the Concepts lite not being implemented in current C++17 version or is this wrong?
If I could setup compiler to use auto as parameter and parameter packs, then that would be best, but if this is not possible, then I will have to rewrite my code to follow C++17 windows standard - any suggestions on how to do this without ending up in a template hell
void addexecutorfunc( Func callback, const auto&...args )
auto as a parameter to a (non-lambda) function is a GNU extension. It is not part of standard C++17, and is not supported by either of the other two major C++ compilers, Clang and MSVC. Rather unfortunately, GCC seems to allow it in -std=c++14 mode as well as with -std=gnu++14.
The standard C++ equivalent would be a function template
template <typename... Ts>
void addexecutorfunc(Func callback, const Ts&... args)
which should work as expected.
I recently ran into a compilation warning (promoted to error) using the 64-bit VS2013 compiler (update 3) making me aware of the existence of pointer type attributes.
The warning indicated a loss of precision as I was about to assign something of type 'Object *' to 'Object * __ptr32'.
I didn't hear about this '__ptr32' suffix yet, so I did some googling and found the following link: http://blog.aaronballman.com/2013/05/msvc-pointer-type-attributes/ .
After reading this, it became clear that I shouldn't simply ignore this warning as I might get ugly bugs in my 64-bit application...
Trying to pinpoint the problem further, I wrote following small program:
#include <type_traits>
struct O { };
struct OD { using V = O *; };
template <typename X, typename Y> struct expect_equal { static_assert(std::is_same<X, Y>::value, "Types do not correspond."); };
template <class D> using VD = typename D::V;
template <class D> struct Exp { template <template <class> class T> using DT = T<D>; };
int main(void)
{
expect_equal<Exp<OD>::DT<VD>, O *> e;
return 0;
}
While it would compile using clang, I got the following error/assertion failure using my MSVC 64-bit compiler (note: the 32-bit compiler did not yield compilation errors):
Microsoft (R) C/C++ Optimizing Compiler Version 18.00.30723 for x64
Copyright (C) Microsoft Corporation. All rights reserved.
test.cpp
test.cpp(6) : error C2338: Types do not correspond.
test.cpp(13) : see reference to class template instantiation 'expect_equal<O * __ptr32,O *>' being compiled
I was wondering whether someone could indicate what I am doing wrong here: I don't use an explicit pointer type attribute anywhere. This also might be a an unspecified behavior or a bug of either the STL implementation or the compiler, but most likely I am missing some important point here... Thanks in advance!
I am reading the chapter "function templates" of C++ Primer 3rd edition, and when I tried to follow the example, I found the code almost the same as the book has encountered an error during compilation under VC6 but everything is ok under g++. I don't know why?
Here is the code:
#include <iostream>
using namespace std;
template<typename T1, typename T2, typename T3>
T1 my_min(T2 a, T3 b)
{
return a>b?b:a;
}
int main()
{
int (*fp)(int, int) = &my_min<int>;
cout<<fp(3,5)<<endl;
return 0;
}
The error happened under VC6 appears like:
error C2440: 'initializing' : cannot convert from '' to 'int (__cdecl *)(int,int)'
None of the functions with this name in scope match the target type
VC6 is an ancient compiler whose support for templates is woefully lacking, so it cannot cope with legal code in many circumstances. You should ditch it and download VS 2010 Express instead.
I am trying to understand how to best make use of blocks in my templated class.
I have the following code snippet:
template<typename T, typename R>
class MyClass {
public:
typedef R (^Block)(T);
MyClass(Block blk) {}
};
void testMyClass() {
MyClass<int,int>::Block blk(^(int arg) {
return 1 + arg;
});
}
When I try to compile this, I get the following error message:
error: cannot convert ‘int (^)(int)’ to ‘R (^)(T)’ in initialization
Am I missing something obvious? Am I trying to do something that is not allowed? GCC accepts the program if I do the same thing without templates.
This is an issue in GCC. I tested your program under LLVM 3 and it compiled just fine.
There are a number of problems in GCC 4.2's implementation of blocks, sometimes they can be worked around by fully qualifying your block declarations ^int(int arg){return 1 + arg;} but in this case it was unable to work around this issue. I would highly recommend moving to use LLVM/Clang for any further work with Obj-C Blocks. Its C++ support is very good these days, especially for C++03 support.