I was converting over some code to use the c++11 chrono library rather than using the ctime library, at least in part to get a better understanding on the chrono library. Most of it has gone great, except for trying to do division by two chrono::durations. I've reduced the offending code down to a simple example and it took me a while to figure out why it was giving me the error it is.
#include <chrono>
using namespace std;
int main()
{
chrono::milliseconds tickLength(16);
chrono::milliseconds futureDuration(200);
auto numTicks = futureDuration / tickLength;
}
This should access the function
template<class _Rep1,
class _Period1,
class _Rep2,
class _Period2> inline
typename common_type<_Rep1, _Rep2>::type
operator/(
const duration<_Rep1, _Period1>& _Left,
const duration<_Rep2, _Period2>& _Right)
but instead appears to be trying to use
template<class _Rep1,
class _Period1,
class _Rep2> inline
typename enable_if<is_convertible<_Rep2,
typename common_type<_Rep1, _Rep2>::type>::value
&& !_Is_duration<_Rep2>::value,
duration<typename common_type<_Rep1, _Rep2>::type, _Period1> >::type
operator/(
const duration<_Rep1, _Period1>& _Left,
const _Rep2& _Right)
and thus is trying to determine a common type between milliseconds and long long. The compiler output is:
1>------ Build started: Project: Playground, Configuration: Debug Win32 ------
1> playground.cpp
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\type_traits(1446): error C2446: ':' : no conversion from 'std::chrono::milliseconds' to '__int64'
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1> c:\users\XXX\documents\visual studio 2013\projects\playground\playground\playground.cpp(9) : see reference to class template instantiation 'std::common_type<__int64,std::chrono::milliseconds>' being compiled
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Am I doing something wrong in my code?
Is this a visual studio issue?
Is this a c++11 standard issue?
Your example code compiles for me using clang/libc++. And your description of what should be happening sounds right to me. Further, if I print out numTicks, I get 12, which is 200/16 (integer division).
Sounds like a visual stdio bug to me. I see nothing wrong with your code.
Related
I made a custom allocator, but my code didn't compile on msvc and I'm not sure if my implementation satisfies the Allocator requirement (disregarding actual behavior of function implementations here). Here is a minimal example that reproduces the error on Visual Studio (16.11 P1 and 16.10):
#include <memory>
#include <vector>
template <typename T>
class Allocator
{
public:
using value_type = T;
[[nodiscard]]
T* allocate(std::size_t n)
{
return nullptr;
}
void deallocate(T* x, std::size_t n)
{
}
constexpr bool operator==(const Allocator& other) const noexcept
{
return true;
}
constexpr bool operator!=(const Allocator& other) const noexcept
{
return !(*this == other);
}
};
int main()
{
using Alloc = Allocator<int>;
using Vec = std::vector<int, Alloc>;
auto vec = Vec();
}
Godbolt isn't complaining for any major compiler but their msvc version is a little behind I think.
To me this looks like a compiler bug in msvc but I want to make sure before I open a ticket.
This is the compiler output:
Build started...
1>------ Build started: Project: Project1, Configuration: Debug x64 ------
1>main.cpp
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\VC\Tools\MSVC\14.29.30129\include\vector(714,27): error C2440: 'static_cast': cannot convert from 'Allocator<int>' to 'Allocator<_Newfirst>'
1> with
1> [
1> _Newfirst=std::_Container_proxy
1> ]
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\VC\Tools\MSVC\14.29.30129\include\vector(714,27): message : No constructor could take the source type, or constructor overload resolution was ambiguous
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\VC\Tools\MSVC\14.29.30129\include\vector(711): message : while compiling class template member function 'std::vector<int,Alloc>::~vector(void) noexcept'
1>C:\code\dumpster\Project1\Project1\main.cpp(36): message : see reference to function template instantiation 'std::vector<int,Alloc>::~vector(void) noexcept' being compiled
1>C:\code\dumpster\Project1\Project1\main.cpp(36): message : see reference to class template instantiation 'std::vector<int,Alloc>' being compiled
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\VC\Tools\MSVC\14.29.30129\include\vector(714,25): error C2530: '_Alproxy': references must be initialized
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\VC\Tools\MSVC\14.29.30129\include\vector(715,1): error C3536: '_Alproxy': cannot be used before it is initialized
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\VC\Tools\MSVC\14.29.30129\include\vector(715,9): error C2672: '_Delete_plain_internal': no matching overloaded function found
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\VC\Tools\MSVC\14.29.30129\include\vector(715,1): error C2893: Failed to specialize function template 'void std::_Delete_plain_internal(_Alloc &,_Alloc::value_type *const ) noexcept'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\VC\Tools\MSVC\14.29.30129\include\xmemory(998): message : see declaration of 'std::_Delete_plain_internal'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\VC\Tools\MSVC\14.29.30129\include\vector(715,1): message : With the following template arguments:
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\VC\Tools\MSVC\14.29.30129\include\vector(715,1): message : '_Alloc=int'
1>Done building project "Project1.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Edit
I opened a bug ticket for the msvc devs.
It does not.
An allocator rebound to a different value type must be constructible from the original allocator - this is the A a(b) row in the requirements you linked.
Your type fails that requirement.
I try to dispatch between two functions using if constexpr. The dispatcher function should accept for example std::size_t and an arbitrary class type.
It works if I just call it with a scalar type, but if I try to pass a class type it triggers a compile error which is not really helpful to me (please see below).
Please have a look at my current approach:
template <auto Other>
constexpr auto mul() const {
if constexpr (std::is_scalar_v<decltype(Other)>)
return mul_with_scalar<Other>();
else
return mul_with_matrix<Other>();
}
template <size_t Scalar>
constexpr auto mul_with_scalar() const {
return apply([](size_t v, auto) { return v * Scalar; },
std::make_index_sequence<Size>{});
}
template <class Other>
constexpr auto mul_with_matrix() const {
return size_t{0}; // implement me
}
note: candidate: template<auto Other> constexpr auto matrix<Rows, Cols, Vals>::mul() const [with auto Other = Other; long unsigned int Rows = 3; long unsigned int Cols = 3; long unsigned int ...Vals = {}]
constexpr auto mul() const {
^~~
./constexpresian/matrix.hpp:81:18: note: template argument deduction/substitution failed:
I wan't that the function mul can handle non-type and type parameters.
That is not possible in C++. A template parameter can be a type or a value (or a template), but it cannot be both. template<auto Name> makes Name a value template parameter, the type of whose value will be deduced at the time the value is passed.
But since it is a compile-time value, you can wrap its value in a type. For integer types, std::integer_constant will work. For future C++ revisions that allow other kinds of value parameters, you'll have to use a more generic wrapper.
There is one question I have, you can refer to my comment below your post that pertains to the term Size.
I tried your code above as is even with Size the way it is to see the compiler errors and this is coming from Visual Studio 2017 v4.6.0105 c++17 on Win 7 x64 Home Premium under x86 Debug on an Intel Quad Core Extreme.
1>------ Build started: Project: StackOverflow, Configuration: Debug Win32 ------
1>Learner.cpp
1>c:\users\skilz80\documents\visual studio 2017\projects\stackoverflow\stackoverflow\learner.h(75): error C3533: a parameter cannot have a type that contains 'auto'
1>c:\users\skilz80\documents\visual studio 2017\projects\stackoverflow\stackoverflow\learner.h(75): error C2270: 'mul': modifiers not allowed on nonmember functions
1>c:\users\skilz80\documents\visual studio 2017\projects\stackoverflow\stackoverflow\learner.h(83): error C2270: 'mul_with_scalar': modifiers not allowed on nonmember functions
1>c:\users\skilz80\documents\visual studio 2017\projects\stackoverflow\stackoverflow\learner.h(89): error C2270: 'mul_with_matrix': modifiers not allowed on nonmember functions
1>Done building project "StackOverflow.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Maybe this interpretation of these compiler errors might help you.
Here are some links that might help concerning auto and template parameters:
StackOverflow : Advantages of auto in template parameters in C++17
open-std.org : Declaring non-type template arguments with auto
www.bfilipek.com : C++17 in details: Templates
I didn't really know what to write in the title, but basically I have a single .cpp, with only standard library headers included and no "using" keywords. I made my own "generate(...)" function. After including the library, Visual Studio shows me an error (where the function is being called), basically saying that it doesn't know whether to choose std::generate(...) or generate(...) because they have matching argument lists.
Is this a bug or have I missed something? I might also add that I am using VS2015.
#include <iostream>
#include <ctime>
#include <vector>
#include <algorithm>
template<typename Iter, typename Function>
Function generate(Iter begin, Iter end, Function f)
{
while (begin != end)
{
*begin = f();
++begin;
}
return f;
}
class Random
{
public:
Random(int low, int high)
: mLow(low), mHigh(high)
{}
int operator()()
{
return mLow + rand() % (mHigh - mLow + 1);
}
private:
int mLow;
int mHigh;
};
class Print
{
void operator()(int t)
{
std::cout << t << " ";
}
};
int main()
{
srand(time(0));
std::vector<int> intVec;
intVec.resize(15);
Random r(2, 7);
generate(intVec.begin(), intVec.end(), r);
}
Error output:
1>------ Build started: Project: Functor, Configuration: Debug Win32 ------
1> Main.cpp
1>c:\users\michael sund\documents\visual studio 2015\projects\gi_cpp\functor\main.cpp(44): warning C4244: 'argument': conversion from 'time_t' to 'unsigned int', possible loss of data
1>c:\users\michael sund\documents\visual studio 2015\projects\gi_cpp\functor\main.cpp(50): error C2668: 'generate': ambiguous call to overloaded function
1> c:\users\michael sund\documents\visual studio 2015\projects\gi_cpp\functor\main.cpp(7): note: could be 'Function generate<std::_Vector_iterator<std::_Vector_val<std::_Simple_types<int>>>,Random>(Iter,Iter,Function)'
1> with
1> [
1> Function=Random,
1> Iter=std::_Vector_iterator<std::_Vector_val<std::_Simple_types<int>>>
1> ]
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\algorithm(1532): note: or 'void std::generate<std::_Vector_iterator<std::_Vector_val<std::_Simple_types<int>>>,Random>(_FwdIt,_FwdIt,_Fn0)' [found using argument-dependent lookup]
1> with
1> [
1> _FwdIt=std::_Vector_iterator<std::_Vector_val<std::_Simple_types<int>>>,
1> _Fn0=Random
1> ]
1> c:\users\michael sund\documents\visual studio 2015\projects\gi_cpp\functor\main.cpp(50): note: while trying to match the argument list '(std::_Vector_iterator<std::_Vector_val<std::_Simple_types<int>>>, std::_Vector_iterator<std::_Vector_val<std::_Simple_types<int>>>, Random)'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
This happens on not just VC++ (VS 2015), but g++ 4.9+ as well. The issue here is the tricky Argument Dependent Lookup (Koenig Lookup).
It looks at the two iterators you're adding and it sees the "generate" function in std because the iterators also come from the std namespace (this is the point of Argument Dependent Lookup).
This problem actually bit me at one point: when I wrote my own tie implementation that did a few things extra to tie. I had to call mine tye because Koenig Lookup caused the considered overloads to be equal in their ranking and thus cause an error like this.
Either prefix generate with :: to start lookup from the global namespace (::generate( vec.begin(), vec.end(), ... );), or name it differently.
I'm trying to fix up a library (entityx) that does not currently compile on Windows using VS 2013. It compiles fine on Linux with gcc and also on Windows with MinGW.
It seems the problem is with SFINAE - I guess VS 2013 doesn't properly ignore substitution failures for templates.
There is a report of this issue on Microsoft Connect, here.
Before I dive into entityx, there is a sample of the problem (taken from the report at Microsoft Connect):
#include <vector>
#include <future>
using namespace std;
typedef int async_io_op;
struct Foo
{
//! Invoke the specified callable when the supplied operation completes
template<class R> inline std::pair < std::vector < future < R >> , std::vector < async_io_op >> call(const std::vector<async_io_op> &ops, const std::vector < std::function < R() >> &callables);
//! Invoke the specified callable when the supplied operation completes
template<class R> std::pair < std::vector < future < R >> , std::vector < async_io_op >> call(const std::vector < std::function < R() >> &callables) { return call(std::vector<async_io_op>(), callables); }
//! Invoke the specified callable when the supplied operation completes
template<class R> inline std::pair<future<R>, async_io_op> call(const async_io_op &req, std::function<R()> callback);
//! Invoke the specified callable when the supplied operation completes
template<class C, class... Args> inline std::pair<future<typename std::result_of<C(Args...)>::type>, async_io_op> call(const async_io_op &req, C callback, Args... args);
};
int main(void)
{
Foo foo;
std::vector<async_io_op> ops;
std::vector < std::function < int() >> callables;
foo.call(ops, std::move(callables));
return 0;
}
I get the following error when attempting to compile this:
error C2064: term does not evaluate to a function taking 0 arguments
c:\program files (x86)\microsoft visual studio 12.0\vc\include\xrefwrap 58
Apparently, we can use std::enable_if to work around this problem. However, I can't figure out how.
Does anyone know how I can fix this compile error?
Edit: Full output from VS 2013:
1>------ Build started: Project: VS2013_SFINAE_Failure, Configuration: Debug Win32 ------
1> test_case.cpp
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\xrefwrap(58): error C2064: term does not evaluate to a function taking 0 arguments
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\xrefwrap(118) : see reference to class template instantiation 'std::_Result_of<_Fty,>' being compiled
1> with
1> [
1> _Fty=std::vector<std::function<int (void)>,std::allocator<std::function<int (void)>>>
1> ]
1> c:\users\jarrett\downloads\vs2013_sfinae_failure\vs2013_sfinae_failure\test_case.cpp(25) : see reference to class template instantiation 'std::result_of<std::vector<std::function<int (void)>,std::allocator<_Ty>> (void)>' being compiled
1> with
1> [
1> _Ty=std::function<int (void)>
1> ]
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
VS2013 compiles the code if you replace std::result_of with its decltype + std::declval equivalent. So change the last Foo::call() definition to
template<class C, class... Args>
inline pair<future<decltype(declval<C>()(declval<Args>()...))>, async_io_op>
call(const async_io_op& req, C callback, Args... args);
If I understand the error correctly, it's related to the defect described here, but then it's surprising that both GCC and clang manage to compile the result_of code without errors.
I am attempting to compile QtScriptGenerator (gitorious) with Visual Studio 2010 (C++) and have run into a compile error. In searching for a solution, I have seen occasional references to compile breakages introduced since VS2008 due to changes in VS2010's implementation of STL and/or c++0x conformance changes.
Any ideas what is happening below, or how I could go about fixing it? If the offending code appeared to be QtScriptGenerator's, I think I would have an easier time fixing it.. but it appears to me that the offending code may be in VS2010's STL implementation and I may be required to create a workaround?
PS. I am pretty unfamiliar with templates and STL. I have a background in embedded and console projects where such things have until recently often been avoided to reduce memory consumption and cross-compiler risks.
Edit - It appears it is probably Visual Studio's implementation of std::copy which changed.
C:\Program Files\Microsoft Visual Studio 10.0\VC\INCLUDE\xutility(275) : error C2679: binary '=' : no operator found which takes a right-hand operand of type 'rpp::pp_output_iterator<_Container>' (or there is no acceptable conversion)
with
[
_Container=std::string
]
c:\qt\qtscriptgenerator\generator\parser\rpp\pp-iterator.h(75): could be 'rpp::pp_output_iterator<_Container> &rpp::pp_output_iterator<_Container>::operator =(const char &)'
with
[
_Container=std::string
]
while trying to match the argument list '(rpp::pp_output_iterator<_Container>, rpp::pp_output_iterator<_Container>)'
with
[
_Container=std::string
]
C:\Program Files\Microsoft Visual Studio 10.0\VC\INCLUDE\xutility(2176) : see reference to function template instantiation '_Iter &std::_Rechecked<_OutIt,_OutIt>(_Iter &,_UIter)' being compiled
with
[
_Iter=rpp::pp_output_iterator<std::string>,
_OutIt=rpp::pp_output_iterator<std::string>,
_UIter=rpp::pp_output_iterator<std::string>
]
c:\qt\qtscriptgenerator\generator\parser\rpp\pp-internal.h(83) : see reference to function template instantiation '_OutIt std::copy<std::_String_iterator<_Elem,_Traits,_Alloc>,_OutputIterator>(_InIt,_InIt,_OutIt)' being compiled
with
[
_OutIt=rpp::pp_output_iterator<std::string>,
_Elem=char,
_Traits=std::char_traits<char>,
_Alloc=std::allocator<char>,
_OutputIterator=rpp::pp_output_iterator<std::string>,
_InIt=std::_String_iterator<char,std::char_traits<char>,std::allocator<char>>
]
c:\qt\qtscriptgenerator\generator\parser\rpp\pp-engine-bits.h(500) : see reference to function template instantiation 'void rpp::_PP_internal::output_line<_OutputIterator>(const std::string &,int,_OutputIterator)' being compiled
with
[
_OutputIterator=rpp::pp_output_iterator<std::string>
]
C:\Program Files\Microsoft Visual Studio 10.0\VC\INCLUDE\xutility(275) : error C2582: 'operator =' function is unavailable in 'rpp::pp_output_iterator<_Container>'
with
[
_Container=std::string
]
Here's some context..
pp-internal.h
#ifndef PP_INTERNAL_H
#define PP_INTERNAL_H
#include <algorithm>
#include <stdio.h>
namespace rpp {
namespace _PP_internal
{
..
64 template <typename _OutputIterator>
65 void output_line(const std::string &__filename, int __line, _OutputIterator __result)
66 {
67 std::string __msg;
68
69 __msg += "# ";
70
71 char __line_descr[16];
72 pp_snprintf (__line_descr, 16, "%d", __line);
73 __msg += __line_descr;
74
75 __msg += " \"";
76
77 if (__filename.empty ())
78 __msg += "<internal>";
79 else
80 __msg += __filename;
81
82 __msg += "\"\n";
83 std::copy (__msg.begin (), __msg.end (), __result);
84 }
pp-engine-bits.h
#ifndef PP_ENGINE_BITS_H
#define PP_ENGINE_BITS_H
#include <stdio.h>
namespace rpp {
450 template <typename _InputIterator, typename _OutputIterator>
451 void pp::operator () (_InputIterator __first, _InputIterator __last, _OutputIterator __result)
452 {
..
497 if (env.current_line != was)
498 {
499 env.current_line = was;
500 _PP_internal::output_line (env.current_file, env.current_line, __result);
501 }
.. and here's the definition of pp_output_iterator
pp-iterator.h
#ifndef PP_ITERATOR_H
#define PP_ITERATOR_H
#include <iterator>
namespace rpp {
..
template <typename _Container>
class pp_output_iterator
: public std::iterator<std::output_iterator_tag, void, void, void, void>
{
std::string &_M_result;
public:
explicit pp_output_iterator(std::string &__result):
_M_result (__result) {}
inline pp_output_iterator &operator=(typename _Container::const_reference __v)
{
if (_M_result.capacity () == _M_result.size ())
_M_result.reserve (_M_result.capacity () << 2);
_M_result.push_back(__v);
return *this;
}
inline pp_output_iterator &operator * () { return *this; }
inline pp_output_iterator &operator ++ () { return *this; }
inline pp_output_iterator operator ++ (int) { return *this; }
};
I think that the problem is that std::copy is trying to use 'copy assignment' (operator=()) on your rpp::pp_output_iterator<> and there is no operator=() for that class template. I should say, there is an operator=() but it doesn't take the correct parameter to be the 'copy assignment' function (ie., it doesn't take a ``rpp::pp_output_iterator<>¶meter). I think that the existence of someoperator=()` function will prevent the compiler from generating a default (I don't have access to the standard document at the moment to verify this 100%).
Note that a type must be assignable (among other things, of course) to be considered an OutputIterator: http://www.sgi.com/tech/stl/OutputIterator.html
Previous versions of std::copy in MSVC might not have actually used assignment (just because OutputIterator must support it doesn't mean std::copy has to use it), which is why it might be a 'new' error in VS2010. (I can't check right now due to limited access to my tools).
I added these code to pp-iterator.h for template class pp_output_iterator, and can fix this issue:
inline pp_output_iterator &operator=(const typename pp_output_iterator<_Container>& __v)
{
_M_result = __v._M_result;
return *this;
}
I had this same problem. The things guesser is saying here did not make much sense to me against the code I got from the git repo (head on 4.7.2011), so I went around this in another way.
This problem is only in the generator project that make a generator that produces the generated code that is utilized by the qtbindings project. The simple fact is that you don't have to use VC10 to build the generator, you can use Qt provided VC9 build to make the generator this will not produce the OP mentioned build errors.
Before running the generator: Remember to change QTDIR to the VC10 Qt build you want the bindings against, or I guess that doesn't matter anyways if the version number is the same. The generator.exe will be built in the release folder, copy that and QtCore and QtXml dll:s to the parent folder from your VC9 Qt that you built the generator against.
After this build the qtbindings project with VC10. I had some minor problems there like it had linker output extensions as .lib, just change all of those to .dll and it should go trough.
Hope this helps people if they have probs with this in the future as Visual Studio 10 gets more traction :) Hopefully the maintainers will fix the build error at some point.