The following code compiles with g++ -std=c++11 but not with clang++ -std=c++11.
Questions
What is the meaning of the single colon "operator" in this context?
Clarification/Edit: How does GCC interpret the code?
How can I make GCC not compile this code? (Assuming that Clang follows the C++ Standard here.) Is there a flag for this?
Code
Compile with g++ -std=c++11 main.cpp and clang++ -std=c++11 main.cpp. I am using GCC 4.8 and Clang 6.0.0 (trunk).
#include <iostream>
#include <vector>
enum Dir { LEFT, RIGHT };
int main(int argc, char** argv) {
// Interesting line: Notice the single ':'
std::vector<Dir> dirs = { Dir:LEFT, Dir:RIGHT };
for (auto v: dirs) {
std::cout << v << std::endl;
}
return 0;
}
Clang Error Message
For completeness and search-ability:
$ clang++ -std=c++11 main.cpp
main.cpp:7:29: warning: use of GNU old-style field designator extension [-Wgnu-designator]
std::vector<Dir> dirs = { Dir:LEFT, Dir:RIGHT };
^~~~
.Dir =
main.cpp:7:39: warning: use of GNU old-style field designator extension [-Wgnu-designator]
std::vector<Dir> dirs = { Dir:LEFT, Dir:RIGHT };
^~~~
.Dir =
main.cpp:7:20: error: no matching constructor for initialization of 'std::vector<Dir>'
std::vector<Dir> dirs = { Dir:LEFT, Dir:RIGHT };
^ ~~~~~~~~~~~~~~~~~~~~~~~
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/bits/stl_vector.h:269:7: note: candidate constructor not viable: cannot convert argument of incomplete type 'void' to 'std::vector::size_type' (aka 'unsigned long') for 1st argument
vector(size_type __n, const allocator_type& __a = allocator_type())
^
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/bits/stl_vector.h:281:7: note: candidate constructor not viable: cannot convert argument of incomplete type 'void' to 'std::vector::size_type' (aka 'unsigned long') for 1st argument
vector(size_type __n, const value_type& __value,
^
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/bits/stl_vector.h:331:7: note: candidate constructor not viable: cannot convert argument of incomplete type 'void' to 'const std::vector<Dir, std::allocator<Dir> >' for 1st argument
vector(const vector& __x, const allocator_type& __a)
^
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/bits/stl_vector.h:340:7: note: candidate constructor not viable: cannot convert argument of incomplete type 'void' to 'std::vector<Dir, std::allocator<Dir> >' for 1st argument
vector(vector&& __rv, const allocator_type& __m)
^
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/bits/stl_vector.h:364:7: note: candidate constructor not viable: cannot convert argument of incomplete type 'void' to 'initializer_list<std::vector<Dir, std::allocator<Dir> >::value_type>'
(aka 'initializer_list<Dir>') for 1st argument
vector(initializer_list<value_type> __l,
^
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/bits/stl_vector.h:392:9: note: candidate template ignored: substitution failure [with _InputIterator = void]: no type named 'iterator_category' in 'std::iterator_traits<void>'
vector(_InputIterator __first, _InputIterator __last,
^
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/bits/stl_vector.h:256:7: note: candidate constructor not viable: requires single argument '__a', but 2 arguments were provided
vector(const allocator_type& __a)
^
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/bits/stl_vector.h:310:7: note: candidate constructor not viable: requires single argument '__x', but 2 arguments were provided
vector(const vector& __x)
^
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/bits/stl_vector.h:327:7: note: candidate constructor not viable: requires single argument '__x', but 2 arguments were provided
vector(vector&& __x) noexcept
^
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/bits/stl_vector.h:248:7: note: candidate constructor not viable: requires 0 arguments, but 2 were provided
vector()
^
2 warnings and 1 error generated.
To cause gcc to reject the code, use -pedantic switch.
The colon is an extension in GNU mode: X:Y means .X = Y, which is a designated initializer. (Neither of these are supported in ISO C++).
gcc also accepts the following code:
std::vector<int> v = { .a = 1, .b = 2 };
but rejects the code:
struct S { int p, q; S() {} };
S s = { .a = 1, .b = 2 }; // S has no member named 'a'
I guess this is a compiler bug; something about initializing a std::vector<int> causes it to ignore the names of the designated initializers. Note, this sort of thing is a hallmark of non-standard features: often the very reason they aren't in the standard is that they didn't mix well with other language features and nobody could come up with a sensible proposal to handle all possible cases.
Related
I want to declare my 2D vector first, then give it a size.
But why I am getting error?
Can anyone explain me?
int main() {
vector<vector<int>> a;
a = vector<int>(16, vector<int>(15));
cout << a.size() << a[0].size();
}
The reason for doing it is that I don't know the size before but after getting the input from the user, I want to give it the size.
Error:
Char 9: error: no matching constructor for initialization of 'vector<int>'
a = vector<int>(16, vector<int>(15));
^ ~~~~~~~~~~~~~~~~~~~
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_vector.h:507:7: note: candidate constructor not viable: no known conversion from 'vector<int>' to 'const std::vector<int, std::allocator<int>>::allocator_type' (aka 'const std::allocator<int>') for 2nd argument
vector(size_type __n, const allocator_type& __a = allocator_type())
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_vector.h:519:7: note: candidate constructor not viable: no known conversion from 'vector<int>' to 'const std::vector<int, std::allocator<int>>::value_type' (aka 'const int') for 2nd argument
vector(size_type __n, const value_type& __value,
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_vector.h:572:7: note: candidate constructor not viable: no known conversion from 'int' to 'const std::vector<int, std::allocator<int>>' for 1st argument
vector(const vector& __x, const allocator_type& __a)
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_vector.h:604:7: note: candidate constructor not viable: no known conversion from 'int' to 'std::vector<int, std::allocator<int>>' for 1st argument
vector(vector&& __rv, const allocator_type& __m)
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_vector.h:622:7: note: candidate constructor not viable: no known conversion from 'int' to 'initializer_list<std::vector<int, std::allocator<int>>::value_type>' (aka 'initializer_list<int>') for 1st argument
vector(initializer_list<value_type> __l,
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_vector.h:650:2: note: candidate template ignored: deduced conflicting types for parameter '_InputIterator' ('int' vs. 'std::vector<int, std::allocator<int>>')
vector(_InputIterator __first, _InputIterator __last,
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_vector.h:494:7: note: candidate constructor not viable: requires single argument '__a', but 2 arguments were provided
vector(const allocator_type& __a) _GLIBCXX_NOEXCEPT
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_vector.h:550:7: note: candidate constructor not viable: requires single argument '__x', but 2 arguments were provided
vector(const vector& __x)
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_vector.h:569:7: note: candidate constructor not viable: requires 1 argument, but 2 were provided
vector(vector&&) noexcept = default;
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_vector.h:582:7: note: candidate constructor not viable: requires 3 arguments, but 2 were provided
vector(vector&& __rv, const allocator_type& __m, true_type) noexcept
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_vector.h:586:7: note: candidate constructor not viable: requires 3 arguments, but 2 were provided
vector(vector&& __rv, const allocator_type& __m, false_type)
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_vector.h:484:7: note: candidate constructor not viable: requires 0 arguments, but 2 were provided
vector() = default;
^
1 error generated.
vector<int> is not a 2D vector.
Instead of this:
a = vector<int>(16, vector<int>(15));
You should use this:
a = vector<vector<int>>(16, vector<int>(15));
I have this c++ program with the following types:
typedef pcl::PointXYZI PointType;
typedef pcl::PointCloud<PointType> PointCloudType;
typedef std::vector<PointCloudType::Ptr> CloudPtrList;
and this in main:
CloudPtrList clusterPtr(2);
for (int i = 0; i < clusterPtr.size(); ++i)
{
clusterPtr[i] = boost::make_shared<PointCloudType>();
}
And i keep getting this error ' no viable overloaded =' when i try to initialize this vector (the for above)
clusterPtr[i] = boost::make_shared<PointCloudType>();
This is the full error:
No viable overloaded '='
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/memory:3737:17: Candidate function not viable: no known conversion from 'typename boost::detail::sp_if_not_array<PointCloud<PointXYZI> >::type' (aka 'shared_ptr<pcl::PointCloud<pcl::PointXYZI> >') to 'const std::__1::shared_ptr<pcl::PointCloud<pcl::PointXYZI> >' for 1st argument
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/memory:3748:17: Candidate function not viable: no known conversion from 'typename boost::detail::sp_if_not_array<PointCloud<PointXYZI> >::type' (aka 'shared_ptr<pcl::PointCloud<pcl::PointXYZI> >') to 'std::__1::shared_ptr<pcl::PointCloud<pcl::PointXYZI> >' for 1st argument
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/memory:3745:9: Candidate template ignored: could not match 'std::__1::shared_ptr' against 'boost::shared_ptr'
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/memory:3756:9: Candidate template ignored: could not match 'std::__1::shared_ptr' against 'boost::shared_ptr'
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/memory:3766:9: Candidate template ignored: could not match 'auto_ptr' against 'shared_ptr'
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/memory:3790:9: Candidate template ignored: could not match 'unique_ptr' against 'shared_ptr'
As can be seen in the error message ...
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/memory:3737:17: Candidate function not viable: no known conversion from 'typename boost::detail::sp_if_not_array<PointCloud<PointXYZI> >::type' (aka 'shared_ptr<pcl::PointCloud<pcl::PointXYZI> >') to 'const std::__1::shared_ptr<pcl::PointCloud<pcl::PointXYZI> >' for 1st argument
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/memory:3748:17: Candidate function not viable: no known conversion from 'typename boost::detail::sp_if_not_array<PointCloud<PointXYZI> >::type' (aka 'shared_ptr<pcl::PointCloud<pcl::PointXYZI> >') to 'std::__1::shared_ptr<pcl::PointCloud<pcl::PointXYZI> >' for 1st argument
...you are using a newer version of PCL in which boost::shared_ptr has been replaced by std::shared_ptr:
#include <pcl/common/projection_matrix.h>
#include <pcl/point_types.h>
//#include <boost/make_shared.hpp> // boost::make_shared
#include <memory> // std::make_shared
typedef pcl::PointCloud<pcl::PointXYZI> PointCloudType;
int main() {
std::vector<PointCloudType::Ptr> clusterPtr(2);
for(int i = 0; i < clusterPtr.size(); ++i) {
clusterPtr[i] = std::make_shared<PointCloudType>(); // std::make_shared
}
}
I would expect that std::reference_wrapper would work as a reference in terms of converting non-const into const, like:
int a = 10;
int& refA = a;
const int& constRefA = refA;
The following code compiles and works fine in MSVC and GCC, but not on Clang. I just don't understand why, is it UB, or actually an issue on Clang compiler?
#include <functional>
#include <optional>
int main()
{
int a = 10;
std::reference_wrapper<int> ref = a;
std::reference_wrapper<const int> constRef = ref;
std::optional<std::reference_wrapper<int>> optRef = a;
std::optional<std::reference_wrapper<const int>> optConstRef = optRef;
return 0;
}
On Clang only, displays the following error:
prog.cc:13:39: error: no viable conversion from 'reference_wrapper<int>' to 'reference_wrapper<const int>'
std::reference_wrapper<const int> constRef = ref;
https://wandbox.org/permlink/FSY4tCvE9B17hbVn
prog.cc:13:39: error: no viable conversion from 'reference_wrapper<int>' to 'reference_wrapper<const int>'
std::reference_wrapper<const int> constRef = ref;
^ ~~~
/opt/wandbox/clang-head/include/c++/v1/__functional_base:374:28: note: candidate constructor (the implicit copy constructor) not viable: no known conversion from 'std::reference_wrapper<int>' to 'const std::reference_wrapper<const int> &' for 1st argument
class _LIBCPP_TEMPLATE_VIS reference_wrapper
^
/opt/wandbox/clang-head/include/c++/v1/__functional_base:374:28: note: candidate constructor (the implicit move constructor) not viable: no known conversion from 'std::reference_wrapper<int>' to 'std::reference_wrapper<const int> &&' for 1st argument
/opt/wandbox/clang-head/include/c++/v1/__functional_base:386:5: note: candidate constructor not viable: no known conversion from 'std::reference_wrapper<int>' to 'std::reference_wrapper<const int>::type &' (aka 'const int &') for 1st argument
reference_wrapper(type& __f) _NOEXCEPT
^
/opt/wandbox/clang-head/include/c++/v1/__functional_base:389:14: note: candidate constructor not viable: no known conversion from 'std::reference_wrapper<int>' to 'std::reference_wrapper<const int>::type &&' (aka 'const int &&') for 1st argument
private: reference_wrapper(type&&); public: // = delete; // do not bind to temps
^
/opt/wandbox/clang-head/include/c++/v1/__functional_base:394:5: note: candidate function
operator type&() const _NOEXCEPT {return *__f_;}
^
prog.cc:16:54: error: no viable conversion from 'optional<reference_wrapper<int>>' to 'optional<reference_wrapper<const int>>'
std::optional<std::reference_wrapper<const int>> optConstRef = optRef;
^ ~~~~~~
/opt/wandbox/clang-head/include/c++/v1/optional:689:41: note: candidate constructor not viable: no known conversion from 'std::optional<std::reference_wrapper<int>>' to 'const std::optional<std::reference_wrapper<const int>> &' for 1st argument
_LIBCPP_INLINE_VISIBILITY constexpr optional(const optional&) = default;
^
/opt/wandbox/clang-head/include/c++/v1/optional:690:41: note: candidate constructor not viable: no known conversion from 'std::optional<std::reference_wrapper<int>>' to 'std::optional<std::reference_wrapper<const int>> &&' for 1st argument
_LIBCPP_INLINE_VISIBILITY constexpr optional(optional&&) = default;
^
/opt/wandbox/clang-head/include/c++/v1/optional:691:41: note: candidate constructor not viable: no known conversion from 'std::optional<std::reference_wrapper<int>>' to 'std::nullopt_t' for 1st argument
_LIBCPP_INLINE_VISIBILITY constexpr optional(nullopt_t) noexcept {}
^
/opt/wandbox/clang-head/include/c++/v1/optional:715:15: note: candidate template ignored: substitution failure [with _Up = std::optional<std::reference_wrapper<int>> &]: no member named '_EnableIfImpl' in 'std::_MetaBase<false>'
constexpr optional(_Up&& __v)
^
/opt/wandbox/clang-head/include/c++/v1/optional:730:5: note: candidate template ignored: substitution failure [with _Up = std::reference_wrapper<int>]: no member named '_EnableIfImpl' in 'std::_MetaBase<false>'
optional(const optional<_Up>& __v)
^
/opt/wandbox/clang-head/include/c++/v1/optional:748:5: note: candidate template ignored: substitution failure [with _Up = std::reference_wrapper<int>]: no member named '_EnableIfImpl' in 'std::_MetaBase<false>'
optional(optional<_Up>&& __v)
^
/opt/wandbox/clang-head/include/c++/v1/optional:701:24: note: explicit constructor is not a candidate
constexpr explicit optional(_InPlaceT, _Args&&... __args)
^
/opt/wandbox/clang-head/include/c++/v1/optional:722:24: note: explicit constructor is not a candidate
constexpr explicit optional(_Up&& __v)
^
/opt/wandbox/clang-head/include/c++/v1/optional:738:14: note: explicit constructor is not a candidate
explicit optional(const optional<_Up>& __v)
^
/opt/wandbox/clang-head/include/c++/v1/optional:756:14: note: explicit constructor is not a candidate
explicit optional(optional<_Up>&& __v)
^
2 errors generated.
The std library you are using on wandbox has a bug. It only found 2 constructors and a conversion operator.
Compilers and standard libraries are not always in sync.
printf("Hello World");
int a = 10;
std::reference_wrapper<int> ref = a;
std::reference_wrapper<const int> constRef( ref );
std::optional<std::reference_wrapper<int>> optRef = a;
std::optional<std::reference_wrapper<const int>> optConstRef( optRef );
return 0;
By making the conversions explicit it works. I don't know why; there aren't any explicit conversions differences in my reading of reference_wrapper construction and conversion operators.
But the lack of implicit reference wrapping conversion would explain why the optional would want it.
In any case, this is clearly a bug. The universal conversion constructor for reference_wrapper<const int> should apply if:
void FUN(int const&) {}
FUN(ref)
overload resolution works, and it does for a reference_wrapper<int>.
I'm trying to implement a feature found in Dolphin emulator into an unofficial build while learning about C++ coding on my own. Right now I was able to decrease the amount of errors to one file but these are the errors that I am getting for it.
First code errors are for this:
const u64 bytes_per_chunk = std::min(out_data_per_chunk, VolumeWii::GROUP_DATA_SIZE);
const u64 total_size = blocks_in_this_group * VolumeWii::BLOCK_DATA_SIZE;
const u64 data_offset = parameters.data_offset + write_offset_of_group;
I get an error that says (C:\Users\Desktop\dolphin\Source\Core\DiscIO\WIABlob.cpp:1465:39: error: no matching function for call to 'min') when referring to the " std::min " It appears in here as well:
const u64 bytes_to_write = std::min(bytes_to_write_total, VolumeWii::GROUP_DATA_SIZE);
Error = (C:\Users\Desktop\dolphin\Source\Core\DiscIO\WIABlob.cpp:1488:42: error: no matching function for call to 'min')
------------------------------------------------------------------------------
The next error I get (again all this is within the same .cpp file) is also from the same lines of code:
const u64 bytes_per_chunk = std::min(out_data_per_chunk, VolumeWii::GROUP_DATA_SIZE);
const u64 total_size = blocks_in_this_group * VolumeWii::BLOCK_DATA_SIZE;
const u64 data_offset = parameters.data_offset + write_offset_of_group;
and here too:
const u64 bytes_to_write = std::min(bytes_to_write_total, VolumeWii::GROUP_DATA_SIZE);
The error I get for the later set is [C:\Users\Desktop\dolphin\Source\Core\DiscIO\WIABlob.cpp:1465:21: error: default initialization of an object of const type 'const u64' (aka 'const unsigned long')] --> this is related/highlighted by the "bytes_to_write" line in Android Studio.
Also appears lower down still related to another "bytes_to_write" line [C:\Users\Desktop\dolphin\Source\Core\DiscIO\WIABlob.cpp:1488:25: error: default initialization of an object of const type 'const u64' (aka 'const unsigned long')]
So far those are the 2 errors that stop me from building the app.
====================================================
Build errors:
In file included from ../../../../../../Core/DiscIO/WIABlob.cpp:5:
In file included from ../../../../../../Core\DiscIO/WIABlob.h:21:
In file included from ../../../../../../Core\DiscIO/WiiEncryptionCache.h:12:
../../../../../../Core\DiscIO/VolumeWii.h:91:3: warning: 'DiscIO::VolumeWii::CheckBlockIntegrity' hides overloaded virtual function [-Woverloaded-virtual]
CheckBlockIntegrity(u64 block_index, const Partition &partition, const Partition partition1) const ;
^
../../../../../../Core\DiscIO/Volume.h:112:16: note: hidden overloaded virtual function 'DiscIO::Volume::CheckBlockIntegrity' declared here: different number of parameters (2 vs 3)
virtual bool CheckBlockIntegrity(u64 block_index, const Partition& partition) const
^
In file included from ../../../../../../Core/DiscIO/WIABlob.cpp:5:
In file included from ../../../../../../Core\DiscIO/WIABlob.h:21:
In file included from ../../../../../../Core\DiscIO/WiiEncryptionCache.h:12:
../../../../../../Core\DiscIO/VolumeWii.h:141:10: warning: 'DiscIO::VolumeWii::CheckBlockIntegrity' hides overloaded virtual function [-Woverloaded-virtual]
bool CheckBlockIntegrity(u64 block_index, const std::vector<u8> &encrypted_data,
^
../../../../../../Core\DiscIO/Volume.h:112:16: note: hidden overloaded virtual function 'DiscIO::Volume::CheckBlockIntegrity' declared here: different number of parameters (2 vs 3)
virtual bool CheckBlockIntegrity(u64 block_index, const Partition& partition) const
^
../../../../../../Core/DiscIO/WIABlob.cpp:1465:39: error: no matching function for call to 'min'
const u64 bytes_per_chunk = std::min(out_data_per_chunk, VolumeWii::GROUP_DATA_SIZE);
^~~~~~~~
C:/Android/SDK/ndk/21.3.6528147/toolchains/llvm/prebuilt/windows-x86_64/sysroot/usr/include/c++/v1\algorithm:2532:1: note: candidate template ignored: deduced conflicting types for parameter '_Tp' ('unsigned long' vs. 'unsigned int')
min(const _Tp& __a, const _Tp& __b)
^
C:/Android/SDK/ndk/21.3.6528147/toolchains/llvm/prebuilt/windows-x86_64/sysroot/usr/include/c++/v1\algorithm:2543:1: note: candidate template ignored: could not match 'initializer_list<type-parameter-0-0>' against 'unsigned long'
min(initializer_list<_Tp> __t, _Compare __comp)
^
C:/Android/SDK/ndk/21.3.6528147/toolchains/llvm/prebuilt/windows-x86_64/sysroot/usr/include/c++/v1\algorithm:2523:1: note: candidate function template not viable: requires 3 arguments, but 2 were provided
min(const _Tp& __a, const _Tp& __b, _Compare __comp)
^
C:/Android/SDK/ndk/21.3.6528147/toolchains/llvm/prebuilt/windows-x86_64/sysroot/usr/include/c++/v1\algorithm:2552:1: note: candidate function template not viable: requires single argument '__t', but 2 arguments were provided
min(initializer_list<_Tp> __t)
^
../../../../../../Core/DiscIO/WIABlob.cpp:1488:42: error: no matching function for call to 'min'
const u64 bytes_to_write = std::min(bytes_to_write_total, VolumeWii::GROUP_DATA_SIZE);
^~~~~~~~
C:/Android/SDK/ndk/21.3.6528147/toolchains/llvm/prebuilt/windows-x86_64/sysroot/usr/include/c++/v1\algorithm:2532:1: note: candidate template ignored: deduced conflicting types for parameter '_Tp' ('unsigned long' vs. 'unsigned int')
min(const _Tp& __a, const _Tp& __b)
^
C:/Android/SDK/ndk/21.3.6528147/toolchains/llvm/prebuilt/windows-x86_64/sysroot/usr/include/c++/v1\algorithm:2543:1: note: candidate template ignored: could not match 'initializer_list<type-parameter-0-0>' against 'unsigned long'
min(initializer_list<_Tp> __t, _Compare __comp)
^
C:/Android/SDK/ndk/21.3.6528147/toolchains/llvm/prebuilt/windows-x86_64/sysroot/usr/include/c++/v1\algorithm:2523:1: note: candidate function template not viable: requires 3 arguments, but 2 were provided
min(const _Tp& __a, const _Tp& __b, _Compare __comp)
^
C:/Android/SDK/ndk/21.3.6528147/toolchains/llvm/prebuilt/windows-x86_64/sysroot/usr/include/c++/v1\algorithm:2552:1: note: candidate function template not viable: requires single argument '__t', but 2 arguments were provided
min(initializer_list<_Tp> __t)
^
IF you want to see the original code that I am using its from this .cpp file here:
https://github.com/JosJuice/dolphin/blob/660d81a10b4a8ff9a61631e69fb93e9010bccbc0/Source/Core/DiscIO/WIABlob.cpp
I hope I can get any good advice to get through this issue and finally built the Apk (got stuck on this for weeks now)
Your error message should be pretty clear. The relevant part:
../../../../../../Core/DiscIO/WIABlob.cpp:1465:39: error: no matching function for call to 'min'
const u64 bytes_per_chunk = std::min(out_data_per_chunk, VolumeWii::GROUP_DATA_SIZE);
^~~~~~~~
C:/Android/SDK/ndk/21.3.6528147/toolchains/llvm/prebuilt/windows-x86_64/sysroot/usr/include/c++/v1\algorithm:2532:1: note: candidate template ignored: deduced conflicting types for parameter '_Tp' ('unsigned long' vs. 'unsigned int')
min(const _Tp& __a, const _Tp& __b)
The key part is that you're passing different types to std::min, which isn't allowed (think about what the return type would be). You'll need to either convert one of your arguments to the other type, or you could be explicit about which version of std::min you want.
The conversion would look like this:
const u64 bytes_per_chunk = std::min(out_data_per_chunk, static_cast<unsigned long>(VolumeWii::GROUP_DATA_SIZE));
The explicit call version would look like this:
const u64 bytes_per_chunk = std::min<u64>(out_data_per_chunk, VolumeWii::GROUP_DATA_SIZE);
When compiling my program with clang++ --std=c++11 file.cpp the line std::unique_ptr<FILE> pipe(popen(cmd.c_str(), "r"), pclose); throws the error
memdiff.cpp:11:27: error: no matching constructor for initialization of
'std::unique_ptr<FILE>'
std::unique_ptr<FILE> pipe(popen(cmd.c_str(), "r"), pclose);
^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/memory:2530:31: note:
candidate constructor not viable: no known conversion from 'int (FILE *)'
to 'const std::__1::default_delete<__sFILE>' for 2nd argument
_LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, typename conditional<
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/memory:2537:31: note:
candidate constructor not viable: no known conversion from 'int (FILE *)'
to 'typename remove_reference<deleter_type>::type' (aka
'std::__1::default_delete<__sFILE>') for 2nd argument
_LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, typename remove_ref...
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/memory:2547:9: note:
candidate template ignored: could not match 'unique_ptr<type-parameter-0-0,
type-parameter-0-1>' against '__sFILE *'
unique_ptr(unique_ptr<_Up, _Ep>&& __u,
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/memory:2562:35: note:
candidate template ignored: could not match 'auto_ptr<type-parameter-0-0>'
against '__sFILE *'
_LIBCPP_INLINE_VISIBILITY unique_ptr(auto_ptr<_Up>&& __p,
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/memory:2516:49: note:
candidate constructor not viable: requires 1 argument, but 2 were provided
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/memory:2522:40: note:
candidate constructor not viable: requires single argument '__p', but 2
arguments were provided
_LIBCPP_INLINE_VISIBILITY explicit unique_ptr(pointer __p) _NOEXCEPT
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/memory:2543:31: note:
candidate constructor not viable: requires single argument '__u', but 2
arguments were provided
_LIBCPP_INLINE_VISIBILITY unique_ptr(unique_ptr&& __u) _NOEXCEPT
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/memory:2487:29: note:
candidate constructor (the implicit copy constructor) not viable: requires
1 argument, but 2 were provided
class _LIBCPP_TYPE_VIS_ONLY unique_ptr
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/memory:2510:49: note:
candidate constructor not viable: requires 0 arguments, but 2 were provided
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT
^
1 error generated.
If I switch from a unique_ptr to a shared_ptr my program compiles. Why does one constructor work and one not, and how do I fix it?
As correctly pointed out in the first comment: With unique_ptr, the type of the deleter must be specified as the second template parameter.
However, it's supposed to be a function pointer:
std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd.c_str(), "r"), pclose);