user-defined conversion cannot use static_cast in C++ - c++

I am trying to solve some problems with Eigen. In the process, I found that static_cast and user-defined conversion conflict, maybe the problem caused by std::enable_if?
Here is basically what I tried:
#include <Eigen/Dense>
#include <bits/stdc++.h>
using namespace std;
class Vector2
{
public:
operator Eigen::Vector2d ()
{
return data;
}
private:
Eigen::Vector2d data;
};
class Box2
{
public:
void Extend(const Vector2 & a)
{
data.extend( static_cast<Eigen::Vector2d>(a) ); // error, but why?
}
private:
Eigen::AlignedBox2d data;
};
int main()
{
Vector2 a;
Eigen::Vector2d b = a; // ok, implicit conversion
return 0;
}
g++ -std=c++11outputs a lot of error logs, this is the last part:
eigen/eigen/Eigen/src/Core/PlainObjectBase.h:863:30: note: template argument deduction/substitution failed:
eigen/eigen/Eigen/src/Core/PlainObjectBase.h: In substitution of ‘template<class T> void Eigen::PlainObjectBase<Derived>::_init1(const Index&, typename Eigen::internal::enable_if<((((((! Eigen::internal::is_same<long int, typename Eigen::internal::traits<T>::Scalar>::value) && Eigen::internal::is_same<long int, T>::value) && (typename Eigen::internal::dense_xpr_base<Derived>::type:: SizeAtCompileTime != Eigen::Dynamic)) && (typename Eigen::internal::dense_xpr_base<Derived>::type:: SizeAtCompileTime != 1)) && Eigen::internal::is_convertible<T, typename Eigen::internal::traits<T>::Scalar>::value) && Eigen::internal::is_same<typename Eigen::internal::traits<T>::XprKind, Eigen::ArrayXpr>::value), T*>::type*) [with T = T; Derived = Eigen::Matrix<double, 2, 1>] [with T = Vector2]’:
eigen/eigen/Eigen/src/Core/Matrix.h:296:33: required from ‘Eigen::Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>::Matrix(const T&) [with T = Vector2; _Scalar = double; int _Rows = 2; int _Cols = 1; int _Options = 0; int _MaxRows = 2; int _MaxCols = 1]’
test.cpp:21:52: required from here
eigen/eigen/Eigen/src/Core/PlainObjectBase.h:863:30: error: invalid use of incomplete type ‘struct Eigen::internal::enable_if<false, Vector2*>’
In file included from eigen/eigen/Eigen/Core:364:0,
from eigen/eigen/Eigen/Dense:1,
from eigen/eigen/Eigen/Eigen:1,
from test.cpp:2:
eigen/eigen/Eigen/src/Core/util/Meta.h:162:50: error: declaration of ‘struct Eigen::internal::enable_if<false, Vector2*>’
template<bool Condition, typename T=void> struct enable_if;
^
It seems like the issue of enable_if, I wonder why static_cast doesn't work. Is there a way to solve this problem without modifying Eigen? It is ok to do this now:
void Extend(Vector2 a)
{
Eigen::Vector2d b = a;
data.extend( b );
}
But it's too ugly.

It seems like the issue of enable_if, I wonder why static_cast doesn't
work.
This has nothing to do with the static_cast itself. The error is generated because Vector2's operator Eigen::Vector2d () isn't defined for const objects and since your Box2::Extend() takes a const Vector2& a, the appropriate casting isn't defined for that line in the code. Add a const version of Vector2's user defined casting to Eigen::Vector2d to make it both compile and keep the const-correctness of the code:
const operator Eigen::Vector2d () const
{
return data;
}
Now the usage is working like you wanted:
int main()
{
Vector2 a;
Eigen::Vector2d b = a; // ok, implicit conversion
Box2 boxy;
boxy.Extend(a); // also ok, implicit conversion inside, from a const&
return 0;
}

Related

C++ Custom class inhereting Eigen initializer list

I am trying to make a custom Vector3 inheriting from Eigen's vector 3 like so.
using Vector3 = Eigen::Vector3d;
enum class CustomEnum
{
E1, E2 E3, E4,
};
class CustomVector3 : public Vector3
{
public:
// Taken from here: https://eigen.tuxfamily.org/dox/TopicCustomizing_InheritingMatrix.html
CustomVector3():Vector3(){}
template<typename OtherDerived>
CustomVector3(const Eigen::MatrixBase<OtherDerived>& other) : Vector3(other) {}
// Initializer Constructor
template<typename OtherDerived>
CustomVector3(const std::initializer_list<OtherDerived>& other) : Vector3(other) {}
template<typename OtherDerived>
CustomVector3& operator=(const Eigen::MatrixBase<OtherDerived>& other){
this->Vector3::operator=(other);
return *this;
}
CustomEnum e = CustomEnum::E1;
};
And the goal is to do the following:
TEST_CASE("Initalizer List Test")
{
CustomVector3 vec = {1.1, 2.2, 3.3};
CHECK(vec.x() == 1.1);
CHECK(vec.y() == 2.2);
CHECK(vec.z() == 3.3);
assert(vec.frame == CustomEnum::E1);
}
Without the initializer constructor, I am able to do the following, but not my goal mentioned above:
Vector3 data = {1.1, 2.2, 3.3};
CustomVector3 framedData = data;
With the initializer constructor, I get the following compile error message (I called it FramedVector3 in my code, but for clarity I called it CustomVector3 in this post):
/usr/include/eigen3/Eigen/src/Core/Matrix.h: In instantiation of ‘Eigen::Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>::Matrix(const T&) [with T = std::initializer_list<double>; _Scalar = double; int _Rows = 3; int _Cols = 1; int _Options = 0; int _MaxRows = 3; int _MaxCols = 1]’:
/home/seedship/uavAP/include/uavAP/Core/FramedVector3.h:23:81: required from ‘FramedVector3::FramedVector3(const std::initializer_list<_Tp>&) [with OtherDerived = double]’
/home/seedship/uavAP/include/uavAP/Core/SensorData.h:128:38: required from here
/usr/include/eigen3/Eigen/src/Core/Matrix.h:294:31: error: no matching function for call to ‘Eigen::Matrix<double, 3, 1>::_init1<std::initializer_list<double> >(const std::initializer_list<double>&)’
294 | Base::template _init1<T>(x);
| ~~~~~~~~~~~~~~~~~~~~~~~~^~~
In file included from /usr/include/eigen3/Eigen/Core:462,
from /usr/include/eigen3/Eigen/Dense:1,
from /usr/local/include/cpsCore/Utilities/LinearAlgebra.h:13,
from /home/seedship/uavAP/include/uavAP/Core/FramedVector3.h:9,
from /home/seedship/uavAP/include/uavAP/Core/SensorData.h:11,
from /home/seedship/uavAP/include/uavAP/Core/Orientation/ENU.h:8,
from /home/seedship/uavAP/src/Core/Orientation/ENU.cpp:5:
/usr/include/eigen3/Eigen/src/Core/PlainObjectBase.h:774:30: note: candidate: ‘void Eigen::PlainObjectBase<Derived>::_init1(Eigen::Index, typename Eigen::internal::enable_if<(((typename Eigen::internal::dense_xpr_base<Derived>::type::SizeAtCompileTime != 1) || (! Eigen::internal::is_convertible<T, typename Eigen::internal::traits<T>::Scalar>::value)) && ((! Eigen::internal::is_same<typename Eigen::internal::traits<T>::XprKind, Eigen::ArrayXpr>::value) || (typename Eigen::internal::dense_xpr_base<Derived>::type::SizeAtCompileTime == Eigen::Dynamic))), T>::type*) [with T = std::initializer_list<double>; Derived = Eigen::Matrix<double, 3, 1>; Eigen::Index = long int; typename Eigen::internal::enable_if<(((typename Eigen::internal::dense_xpr_base<Derived>::type::SizeAtCompileTime != 1) || (! Eigen::internal::is_convertible<T, typename Eigen::internal::traits<T>::Scalar>::value)) && ((! Eigen::internal::is_same<typename Eigen::internal::traits<T>::XprKind, Eigen::ArrayXpr>::value) || (typename Eigen::internal::dense_xpr_base<Derived>::type::SizeAtCompileTime == Eigen::Dynamic))), T>::type = std::initializer_list<double>]’
774 | EIGEN_STRONG_INLINE void _init1(Index size, typename internal::enable_if< (Base::SizeAtCompileTime!=1 || !internal::is_convertible<T, Scalar>::value)
| ^~~~~~
/usr/include/eigen3/Eigen/src/Core/PlainObjectBase.h:774:43: note: no known conversion for argument 1 from ‘const std::initializer_list<double>’ to ‘Eigen::Index’ {aka ‘long int’}
774 | EIGEN_STRONG_INLINE void _init1(Index size, typename internal::enable_if< (Base::SizeAtCompileTime!=1 || !internal::is_convertible<T, Scalar>::value)
| ~~~~~~^~~~
/usr/include/eigen3/Eigen/src/Core/PlainObjectBase.h:788:30: note: candidate: ‘template<class T> void Eigen::PlainObjectBase<Derived>::_init1(const Scalar&, typename Eigen::internal::enable_if<((typename Eigen::internal::dense_xpr_base<Derived>::type::SizeAtCompileTime == 1) && Eigen::internal::is_convertible<T, typename Eigen::internal::traits<T>::Scalar>::value), T>::type*) [with T = T; Derived = Eigen::Matrix<double, 3, 1>]’
788 | EIGEN_STRONG_INLINE void _init1(const Scalar& val0, typename internal::enable_if<Base::SizeAtCompileTime==1 && internal::is_convertible<T, Scalar>::value,T>::type* = 0)
| ^~~~~~
/usr/include/eigen3/Eigen/src/Core/PlainObjectBase.h:788:30: note: template argument deduction/substitution failed:
/usr/include/eigen3/Eigen/src/Core/PlainObjectBase.h: In substitution of ‘template<class T> void Eigen::PlainObjectBase<Eigen::Matrix<double, 3, 1> >::_init1<T>(const Scalar&, typename Eigen::internal::enable_if<((Eigen::MatrixBase<Eigen::Matrix<double, 3, 1> >::SizeAtCompileTime == 1) && Eigen::internal::is_convertible<T, double>::value), T>::type*) [with T = std::initializer_list<double>]’:
/usr/include/eigen3/Eigen/src/Core/Matrix.h:294:31: required from ‘Eigen::Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>::Matrix(const T&) [with T = std::initializer_list<double>; _Scalar = double; int _Rows = 3; int _Cols = 1; int _Options = 0; int _MaxRows = 3; int _MaxCols = 1]’
/home/seedship/uavAP/include/uavAP/Core/FramedVector3.h:23:81: required from ‘FramedVector3::FramedVector3(const std::initializer_list<_Tp>&) [with OtherDerived = double]’
/home/seedship/uavAP/include/uavAP/Core/SensorData.h:128:38: required from here
/usr/include/eigen3/Eigen/src/Core/PlainObjectBase.h:788:30: error: invalid use of incomplete type ‘struct Eigen::internal::enable_if<false, std::initializer_list<double> >’
[Truncated for brevity]
Can anyone explain what I am missing? I'm confused why the assignment operator works, but not the constructor. This happens on both Eigen 3.3.9-1 and eigen-git.
Update: maybe this question is very library specific, so I asked on the Eigen gitlab project (https://gitlab.com/libeigen/eigen/-/issues/2192)
You need to use an extra pair of parenthesis. Also there is no point in templating this constructor since the template argument type needs to match the scalar type of the Eigen vector and if you'd like the scalar type to be configurable you'd need to template the whole class.
In summary
CustomVector3(const std::initializer_list<double>& other) : Vector3({other}) {}
is the correct way to do this. The reason is that Eigen::Vector3d is a typedef for Eigen::Matrix<double, 3, 1>. The initializer list constructor for matrices needs to handle multiple rows and columns however and has thus the signature
explicit PlainObjectBase(const std::initializer_list<std::initializer_list<Scalar>>& list);
If you always want to use 3D vectors the (easier) alternative is to use Eigen's 3D constructor and aggregate initialization:
CustomVector3(double x, double y, double z) : Vector3(x, y, z) {}

How can I create a vector based on a variable type

I'm using a template function, which the goal is reciever a vector and a function, and return the function type.
template <typename T, typename Function>
auto apply(const std::vector<T>& V, const Function &F){
vector<Function> x; # ERROR HERE
return x;
}
But the IDE give me error (http://coliru.stacked-crooked.com/a/ee6ce2127e013a18):
/usr/local/include/c++/10.2.0/ext/new_allocator.h: In instantiation of 'class __gnu_cxx::new_allocator<double(double)>':
/usr/local/include/c++/10.2.0/bits/allocator.h:116:11: required from 'class std::allocator<double(double)>'
/usr/local/include/c++/10.2.0/bits/stl_vector.h:87:21: required from 'struct std::_Vector_base<double(double), std::allocator<double(double)> >'
/usr/local/include/c++/10.2.0/bits/stl_vector.h:389:11: required from 'class std::vector<double(double), std::allocator<double(double)> >'
main.cpp:10:22: required from 'auto apply(const std::vector<T>&, const Function&) [with T = int; Function = double(double)]'
main.cpp:19:39: required from here
/usr/local/include/c++/10.2.0/ext/new_allocator.h:96:7: error: 'const _Tp* __gnu_cxx::new_allocator<_Tp>::address(__gnu_cxx::new_allocator<_Tp>::const_reference) const [with _Tp = double(double); __gnu_cxx::new_allocator<_Tp>::const_pointer = double (*)(double); __gnu_cxx::new_allocator<_Tp>::const_reference = double (&)(double)]' cannot be overloaded with '_Tp* __gnu_cxx::new_allocator<_Tp>::address(__gnu_cxx::new_allocator<_Tp>::reference) const [with _Tp = double(double); __gnu_cxx::new_allocator<_Tp>::pointer = double (*)(double); __gnu_cxx::new_allocator<_Tp>::reference = double (&)(double)]'
96 | address(const_reference __x) const _GLIBCXX_NOEXCEPT
| ^~~~~~~
/usr/local/include/c++/10.2.0/ext/new_allocator.h:92:7: note: previous declaration '_Tp* __gnu_cxx::new_allocator<_Tp>::address(__gnu_cxx::new_allocator<_Tp>::reference) const [with _Tp = double(double); __gnu_cxx::new_allocator<_Tp>::pointer = double (*)(double); __gnu_cxx::new_allocator<_Tp>::reference = double (&)(double)]'
92 | address(reference __x) const _GLIBCXX_NOEXCEPT
| ^~~~~~~
main.cpp: In function 'int main(int, char**)':
main.cpp:19:31: error: conversion from 'vector<double(double),allocator<double(double)>>' to non-scalar type 'vector<double,allocator<double>>' requested
19 | vector<double> r = ::apply(v, seno);
| ~~~~~~~^~~~~~~~~
This is call of the main function.
double seno( double n ) { return sin(n); }
int main( int argc, char* argv[]) {
vector<int> v{ 1, 2, 3, 4, 5 };
vector<double> r = ::apply(v, seno);
cout << r;
return 0;
}
I don't know what I'm doing wrong, so How can I improve this method and pass trough this error?
EDIT: The purpse to generalize the in method insted of using double in the vector is because I want o re-use in another way. So I've generalize the most that I can.
vector<Function> x; // ERROR HERE defines a vector of function pointers. But that's not what you want - you want a vector of the return type of the function. And that's what decltype() is for.
In your apply function, F is the function to be called and T is the type of the values in the vector being passed in. That means T() is the default value of the items in the vector (in this case the default value of int is 0). Then, F(T()) would actually call the function with 0 and return something so decltype(F(T())) tells you the type of the thing returned.
That means you need to write vector<decltype(F(T()))> x; instead.
T() works because the type is int and it is default constructible. As #alterigel said in the comments std::declval<T>() is better when the type is not default constructible.
So vector<decltype(F(std::declval<T>()))> x; might be needed in some situations.
The whole program would look like:
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
template <typename T, typename Function>
auto apply(const std::vector<T>& V, const Function &F) {
vector<decltype(F(T()))> x;
for(auto a : V)
x.push_back(F(a));
return x;
}
double seno( double n ) { return sin(n); }
int main( int argc, char* argv[]) {
vector<int> v{ 1, 2, 3, 4, 5 };
vector<double> r = ::apply(v, seno);
for (auto a : r)
cout << a << " ";
return 0;
}
Try it here: https://onlinegdb.com/SknTsVaHO

operator== does not compile if I include <iostream>

The following code compiles perfectly if:
I don't include <iostream> or
I name operator== as alp::operator==.
I suppose there is a problem with <iostream> and operator==, but I don't know what.
I compile the code with gcc 7.3.0, clang++-6.0 and goldbolt. Always the same error.
The problem is that the compiler is trying to cast the parameters of operator== to const_iterator, but why? (I suppose the compiler doesn't see my version of operator==, and looks for other versions).
#include <vector>
#include <iostream> // comment and compile
namespace alp{
template <typename It_base>
struct Iterator {
using const_iterator = Iterator<typename It_base::const_iterator>;
operator const_iterator() { return const_iterator{}; }
};
template <typename It_base>
bool operator==(const Iterator<It_base>& x, const Iterator<It_base>& y)
{ return true;}
}// namespace
struct Func{
int& operator()(int& p) const {return p;}
};
template <typename It, typename View>
struct View_iterator_base{
using return_type = decltype(View{}(*It{}));
using const_iterator =
View_iterator_base<std::vector<int>::const_iterator, Func>;
};
using view_it =
alp::Iterator<View_iterator_base<std::vector<int>::iterator, Func>>;
int main()
{
view_it p{};
view_it z{};
bool x = operator==(z, p); // only compiles if you remove <iostream>
bool y = alp::operator==(z,p); // always compile
}
Error message:
yy.cpp: In instantiation of ‘struct View_iterator_base<__gnu_cxx::__normal_iterator<const int*, std::vector<int> >, Func>’:
yy.cpp:9:73: required from ‘struct alp::Iterator<View_iterator_base<__gnu_cxx::__normal_iterator<const int*, std::vector<int> >, Func> >’
yy.cpp:44:29: required from here
yy.cpp:28:42: error: no match for call to ‘(Func) (const int&)’
using return_type = decltype(View{}(*It{}));
~~~~~~^~~~~~~
yy.cpp:22:10: note: candidate: int& Func::operator()(int&) const <near match>
int& operator()(int& p) const {return p;}
^~~~~~~~
yy.cpp:22:10: note: conversion of argument 1 would be ill-formed:
yy.cpp:28:42: error: binding reference of type ‘int&’ to ‘const int’ discards qualifiers
using return_type = decltype(View{}(*It{}));
~~~~~~^~~~~~~
I've made a more minimal test case here: https://godbolt.org/z/QQonMG .
The relevant details are:
A using type alias does not instantiate a template. So for example:
template<bool b>
struct fail_if_true {
static_assert(!b, "template parameter must be false");
};
using fail_if_used = fail_if_true<true>;
will not cause a compile time error (if fail_if_used isn't used)
ADL also inspects template parameter classes. In this case, std::vector<int>::iterator is __gnu_cxx::__normal_iterator<const int*, std::vector<int> >, Func>, which has a std::vector<int> in it's template. So, operator== will check in the global namespace (always), alp (As alp::Iterator is in alp), __gnu_cxx and std.
Your View_iterator_base::const_iterator is invalid. View_iterator_base::const_interator::result_type is defined as decltype(Func{}(*std::vector<int>::const_iterator{})). std::vector<int>::const_iterator{} will be a vectors const iterator, so *std::vector<int>::const_iterator{} is a const int&. Func::operator() takes an int&, so this means that the expression is invalid. But it won't cause a compile time error if not used, for the reasons stated above. This means that your conversion operator is to an invalid type.
Since you don't define it as explicit, the conversion operator (To an invalid type) will be used to try and match it to the function parameters if they don't already match. Obviously this will finally instantiate the invalid type, so it will throw a compile time error.
My guess is that iostream includes string, which defines std::operator== for strings.
Here's an example without the std namespace: https://godbolt.org/z/-wlAmv
// Avoid including headers for testing without std::
template<class T> struct is_const { static constexpr const bool value = false; } template<class T> struct is_const<const T> { static constexpr const bool value = true; }
namespace with_another_equals {
struct T {};
bool operator==(const T&, const T&) {
return true;
}
}
namespace ns {
template<class T>
struct wrapper {
using invalid_wrapper = wrapper<typename T::invalid>;
operator invalid_wrapper() {}
};
template<class T>
bool operator==(const wrapper<T>&, const wrapper<T>&) {
return true;
}
}
template<class T>
struct with_invalid {
static_assert(!is_const<T>::value, "Invalid if const");
using invalid = with_invalid<const T>;
};
template<class T>
void test() {
using wrapped = ns::wrapper<with_invalid<T>>;
wrapped a;
wrapped b;
bool x = operator==(a, b);
bool y = ns::operator==(a, b);
}
template void test<int*>();
// Will compile if this line is commented out
template void test<with_another_equals::T>();
Note that just declaring operator const_iterator() should instantiate the type. But it doesn't because it is within templates. My guess is that it is optimised out (where it does compile because it's unused) before it can be checked to show that it can't compile (It doesn't even warn with -Wall -pedantic that it doesn't have a return statement in my example).

C++-14 using enable_if_t to select member function of class templated on integral type

I am trying to enable different member functions based on an integral class template parameter like so:
#include <type_traits>
template<int Dimension>
struct Foo
{
template<std::enable_if_t<Dimension == 1> = 0>
int bar(int i) const { return i; }
template<std::enable_if_t<Dimension == 2> = 0>
int bar(int i, int j) const { return i + j; }
};
int main(int argc, const char **argv)
{
Foo<1> a;
a.bar(1);
Foo<2> b;
b.bar(1,2);
return 0;
}
Using gcc5 in c++-14 mode, it fails to compile with the following errors:
tools/t1.cpp: In instantiation of 'struct Foo<1>':
tools/t1.cpp:18:12: required from here
tools/t1.cpp:13:9: error: no type named 'type' in 'struct std::enable_if<false, int>'
int bar(int i, int j) const { return i + j; }
^
tools/t1.cpp: In instantiation of 'struct Foo<2>':
tools/t1.cpp:21:12: required from here
tools/t1.cpp:10:9: error: no type named 'type' in 'struct std::enable_if<false, int>'
int bar(int i) const { return i; }
These seem to indicated the SFINAE is not doing what I expect since the enable_if_t seems to be working correctly.
In this trivial example, overloading would work as well, but in my actual use case I need to hide the functions to prevent accidental use and / or compilation errors depending on the circumstance.
What am I missing with SFINAE here?
Substitution failure is not an elephant when it happens during template argument deduction.
Also, enable_if_t<true> is void, and you can't have a void template non-type parameter.
Defer evaluation with a default template argument:
template<int Dimension>
struct Foo
{
template<int..., int I = Dimension, std::enable_if_t<I == 1, int> = 0>
int bar(int i) const { return i; }
// etc.
};
The unnamed parameter packint... guards against attempts to explicitly specify I.

c++ compile error with template when using self-defined "bind3rd" function

I want to write a function like "bind1st, bind2nd" to support multifields sort by using stl sort. Can't using std:bind since my g++ version is 3.4.5
It is called like this:
#include <alogritm>
std::sort(begin(), end(), bind3rd(SortCond(), indicate));
My template function is:
#ifndef __INCLUDE_TRIPLE_FUNCTION_H_
#define __INCLUDE_TRIPLE_FUNCTION_H_
#define TRIPLE_ARG(Operation, Type) Operation::Type
// Define the triple_function prototype
template<class Arg1, class Arg2, class Arg3, class Result>
struct triple_function
{
// Define the argument type alias
typedef Arg1 first_argument_type;
typedef Arg2 second_argument_type;
typedef Arg3 third_argument_type;
typedef Result result_type;
};
template <class Operation>
class binder3rd : public binary_function<typename TRIPLE_ARG(Operation, first_argument_type),
typename TRIPLE_ARG(Operation, second_argument_type), typename TRIPLE_ARG(Operation, result_type)>
{
protected:
Operation m_op;
typename Operation::third_argument_type value;
public:
binder3rd(const Operation& x, const typename Operation::third_argument_type y):m_op(x), value(y){}
// Convert this function to binary_function using the third argment
typename Operation::result_type operator()(const typename Operation::first_argument_type& x, const typename Operation::second_argument_type& y) const
{
return m_op(x, y, value);
}
};
// bind3rd function implementation
template<class Operation, class Arg>
inline binder3rd<Operation> bind3rd(const Operation& fn, const Arg& x)
{
return binder3rd<Operation>(fn, x);
}
#endif //__INCLUDE/TRIPLE_FUNCTION_H_
/* vim: set expandtab ts=4 sw=4 sts=4 tw=100: */
And My test cpp file is:
#include <algorithm>
#include <cstdlib>
#include "triple_function.h"
using namespace std;
class TestClass{
public:
int arr[16];
TestClass() {
for (int i = 0; i < 16; ++i) {
arr[i] = rand() % 100;
}
}
};
// sort by which fields
class Indicate {
public:
int ind[16];
};
struct SortA : public triple_function < TestClass, TestClass, const Indicate, bool >
{
bool operator () (const TestClass& a, const TestClass& b,
const Indicate& indicate) const
{
for (int i = 0; i < 16; ++i) {
int pos = indicate.ind[i];
if (a.arr[pos] == b.arr[pos]) {
continue ;
}
return a.arr[pos] < b.arr[pos];
}
return false;
}
};
int main() {
TestClass a[10];
Indicate ind;
for(int i = 0; i < 16; ++i) {
ind.ind[i] = i;
}
sort(a, a+10, bind3rd(SortA(), ind));
// bind3rd(SortA, ind);
}
when using g++ to compile, I got these compile error:
What's wrong with my code?
In file included from test.cpp:19:
triple_function.h:32: error: expected template-name before '<' token
triple_function.h:32: error: expected `{' before '<' token
triple_function.h:32: error: expected unqualified-id before '<' token
test.cpp: In function `int main()':
test.cpp:62: error: invalid use of undefined type `class binder3rd<SortA>'
triple_function.h:32: error: declaration of `class binder3rd<SortA>'
triple_function.h: In function `binder3rd<Operation> bind3rd(const Operation&, const Arg&) [with Operation = SortA, Arg = Indicate]':
test.cpp:62: instantiated from here
triple_function.h:52: error: return type `class binder3rd<SortA>' is incomplete
triple_function.h:53: error: invalid use of undefined type `class binder3rd<SortA>'
triple_function.h:32: error: declaration of `class binder3rd<SortA>'
The compiler is tripping over your binary_function base class. If you mean std::binary_function, then #include <functional> and the std:: prefix. Of course, since you're not actually defining a binary function, I'm not sure the base class is actually appropriate...
As others have said, std::bind is a better solution to this problem if you can use C++11.
Using std::bind instead, you could do e.g. like this:
#include <functional>
...
bool my_compare(const TestClass& a, const TestClass& b,
const Indicate& indicate)
{
...
}
int main()
{
...
using namespace std::placeholders;
std::sort(a, a+10, std::bind(my_compare, _1, _2, ind));
}