Call to the extract function below does not work for me on g++ 4.9.0 (20130421). The error I get is that s1 is not a constant expression. If i can be initialized as constexpr then j and k should too. Is that wrong?
#include <tuple>
template <unsigned N1, unsigned N2>
constexpr bool strmatch(const char (&s1)[N1], const char (&s2)[N2], unsigned i = 0)
{
return (s1[i]==s2[i]) ?
(s1[i]=='\0') ?
true
: strmatch(s1, s2, i+1)
: false;
}
template<unsigned N>
constexpr int extract(const std::tuple<int, int> & t1, const char (&array)[N]) {
return std::get<strmatch(array, "m0")>(t1);
}
int main(void)
{
constexpr int i = strmatch("m0", "m0"); // OK
constexpr int j = extract(std::make_tuple(10, 20), "m0");
constexpr int k = extract(std::make_tuple(10, 20), "m1");
return 0;
}
Your code is ill-formed. The problem is that array is not a core constant expression, so can't be used in the template argument in the call to std::get:
template<unsigned N>
constexpr int extract(const std::tuple<int, int> & t1, const char (&array)[N]) {
return std::get<strmatch(array, "m0")>(t1);
}
Remember that constexpr functions can be called at runtime: this code would use the value of a runtime parameter to this function (array) during translation (in the evaluation of the call to strmatch).
Related
I have these arrays declared globally
char str1[] = "6541254939322816220209974565477289648317";
char str2[] = "3142522751761601737419090933147067701840";
int str1_size = strlen(str1);
int str2_size = strlen(str2);
int lcs[str1_size][str2_size];
int arrows[str1_size][str2_size];
But when I access this inside a function, it gives
lcs was not declared in this scope
also
array bound is not an integer constant before ‘]’ token
int lcs[str1_size][str2_size];
What am I doing wrong here ?
First of all
int a[dim];
where dim isn't a compile-time know constant (constexpr by example) isn't C++ standard; maybe it's possible with some extension with some compiler but isn't C++ standard.
So, in your case
int lcs[str1_size][str2_size];
int arrows[str1_size][str2_size];
where str1_size and str2_size are normal (not constexpr, not const initialized with literals) variables, is an error.
If you could redefine str1_size and str2_size as follows
constexpt int str1_size = strlen(str1);
constexpr int str2_size = strlen(str2);
the lcs and arrows definition could works.
Unfortunately (1) str1 and str2 aren't constexpr and (2) std::strlen() isn't constexpr.
But if declare str1/str2 constexpr and write a constexpr alternative to std::strlen...
#include <iostream>
constexpr std::size_t lenStr (char const * str)
{
std::size_t ret{};
while ( *(str++) )
++ret;
return ret;
}
constexpr char str1[] = "6541254939322816220209974565477289648317";
constexpr char str2[] = "3142522751761601737419090933147067701840";
constexpr auto str1_size = lenStr(str1);
constexpr auto str2_size = lenStr(str2);
int lcs[str1_size][str2_size];
int arrows[str1_size][str2_size];
int main ()
{
}
Unfortunately the preceding code require C++14 (in C++11 it's impossible write so complex constexpr functions).
In C++11 you should write lenStr() in a recursive way
constexpr std::size_t lenStr (char const * str, std::size_t ret = 0u)
{ return *str ? lenStr(++str, ++ret) : ret; }
I am currently using C++14.
I would like to define a Matrix class which I can use for defining runtime matrices, but also constexpr matrices. I also would like to define static constexpr matrices based on such a class.
I consider this as a starting point for the Matrix class.
Then I would like to write something as:
static constexpr Matrix<double,2,2> staticmat{0.1,0.2,0.3,0.4};
so that staticmat is constexpr and unique, being static.
However, in order to initialise this, I would need a constexpr array or a constexpr initialiser list (not implemented in the link I posted, but not much would change). So I could write something like:
static constexpr std::array<double,4> staticmattmp{0.1,0.2,0.3,0.4};
static constexpr Matrix<double,2,2> staticmat(staticmattmp);
This would be ugly because I have to define two things just for one, but, if it worked, I could accept it. Unfortunately the compiler says unknown type name 'staticmattmp'.
How can I solve this, maybe in an elegant way?
How can I solve this, maybe in an elegant way?
I don't know if it's elegant but... with a little work...
First of all, define the following using
template <typename T, std::size_t>
using getType = T;
Next re-declare (declare only; not define) Matrix as follows
template <typename, std::size_t NR, std::size_t NC,
typename = std::make_index_sequence<NR*NC>>
class Matrix;
Now declare your Matrix as a class partial specialization adding a constructor that receive NR*NC elements of type T and use them to initialize the internal std::array
template <typename T, std::size_t NR, std::size_t NC, std::size_t ... Is>
class Matrix<T, NR, NC, std::index_sequence<Is...>>
{
public:
using value_type = T;
constexpr Matrix (getType<value_type, Is> ... vals)
: values_{{vals...}}
{}
// other member and methods
};
But don't forget to declare as default the destructor (maybe also constructor and operator=()).
The following is a full compiling C++14 example
#include <array>
#include <type_traits>
template <typename T, std::size_t>
using getType = T;
template <typename, std::size_t NR, std::size_t NC,
typename = std::make_index_sequence<NR*NC>>
class Matrix;
template <typename T, std::size_t NR, std::size_t NC, std::size_t ... Is>
class Matrix<T, NR, NC, std::index_sequence<Is...>>
{
public:
using value_type = T;
constexpr Matrix (getType<value_type, Is> ... vals)
: values_{{vals...}}
{}
constexpr Matrix (std::array<T, NR*NC> const & a)
: values_{a}
{}
constexpr Matrix (std::array<T, NR*NC> && a)
: values_{std::move(a)}
{}
constexpr Matrix () = default;
~Matrix() = default;
constexpr Matrix (Matrix const &) = default;
constexpr Matrix (Matrix &&) = default;
constexpr Matrix & operator= (Matrix const &) = default;
constexpr Matrix & operator= (Matrix &&) = default;
constexpr T const & operator() (std::size_t r, std::size_t c) const
{ return values_[r*NC+c]; }
T & operator() (std::size_t r, std::size_t c)
{ return values_[r*NC+c]; }
constexpr std::size_t rows () const
{ return NR; }
constexpr std::size_t columns () const
{ return NC; }
private:
std::array<T, NR*NC> values_{};
};
int main()
{
static constexpr Matrix<double,2,2> staticmat{0.1,0.2,0.3,0.4};
}
I am trying to sort cakeTypes vector by the size of their weight. But getting the error in sort implementation.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class CakeType
{
public:
const unsigned int weight_;
const unsigned int value_;
CakeType(unsigned int weight = 0, unsigned int value = 0) :
weight_(weight),
value_(value)
{}
};
bool compareCakes(const CakeType& cake1, const CakeType& cake2) {
return cake1.weight_ < cake2.weight_;
}
unsigned long long maxDuffelBagValue(const std::vector<CakeType>& cakeTypes,
unsigned int weightCapacity)
{
// calculate the maximum value that we can carry
unsigned cakeTypesSize = cakeTypes.size();
unsigned long long valueCalculator[weightCapacity+1][cakeTypesSize+1];
for (unsigned int i = 0; i<=weightCapacity+1; i++) {
valueCalculator[i][0] = 0;
}
for (unsigned int i = 0; i<=cakeTypesSize+1; i++) {
valueCalculator[0][i] = 0;
}
vector<CakeType> sortedCakeTypes(cakeTypes);
sort(sortedCakeTypes.begin(), sortedCakeTypes.end(), compareCakes);
return 0;
}
This is part of there error:
exited with non-zero code (1).
In file included from solution.cc:1:
In file included from /usr/include/c++/v1/iostream:38:
In file included from /usr/include/c++/v1/ios:216:
In file included from /usr/include/c++/v1/__locale:15:
In file included from /usr/include/c++/v1/string:439:
/usr/include/c++/v1/algorithm:3856:17: error: no matching function for call to 'swap'
swap(*__first, *__last);
^~~~
I tried this solution sort() - No matching function for call to 'swap', but it is not the same issue.
Data type which is used by swap function in sort algorithm must be MoveAssignable, then you can perform operation like below
CakeType c1, c2;
c1 = move(c2); // <- move c2 to c1
But in your case CakeType has const data members. You can assign values to const data members only in constructors. Code cannot be compiled because default move/copy assignment operator can't be generated by this restriction (assignment to const member is illegal).
Remove const specifier from your class definition and code will work.
class CakeType
{
public:
unsigned int weight_;
unsigned int value_;
CakeType(unsigned int weight = 0, unsigned int value = 0) :
weight_(weight),
value_(value)
{}
};
I am trying to use recursion to solve this problem where if i call
decimal<0,0,1>();
i should get the decimal number (4 in this case).
I am trying to use recursion with variadic templates but cannot get it to work.
Here's my code;
template<>
int decimal(){
return 0;
}
template<bool a,bool...pack>
int decimal(){
cout<<a<<"called"<<endl;
return a*2 + decimal<pack...>();
};
int main(int argc, char *argv[]){
cout<<decimal<0,0,1>()<<endl;
return 0;
}
What would be the best way to solve this?
template<typename = void>
int decimal(){
return 0;
}
template<bool a,bool...pack>
int decimal(){
cout<<a<<"called"<<endl;
return a + 2*decimal<pack...>();
};
The problem was with the recursive case, where it expects to be able to call decltype<>(). That is what I have defined in the first overload above. You can essentially ignore the typename=void, the is just necessary to allow the first one to compile.
A possible solution can be the use of a constexpr function (so you can use it's values it's value run-time, when appropriate) where the values are argument of the function.
Something like
#include <iostream>
constexpr int decimal ()
{ return 0; }
template <typename T, typename ... packT>
constexpr int decimal (T const & a, packT ... pack)
{ return a*2 + decimal(pack...); }
int main(int argc, char *argv[])
{
constexpr int val { decimal(0, 0, 1) };
static_assert( val == 2, "!");
std::cout << val << std::endl;
return 0;
}
But I obtain 2, not 4.
Are you sure that your code should return 4?
-- EDIT --
As pointed by aschepler, my example decimal() template function return "eturns twice the sum of its arguments, which is not" what do you want.
Well, with 0, 1, true and false you obtain the same; with other number, you obtain different results.
But you can modify decimal() as follows
template <typename ... packT>
constexpr int decimal (bool a, packT ... pack)
{ return a*2 + decimal(pack...); }
to avoid this problem.
This is a C++14 solution. It is mostly C++11, except for std::integral_sequence nad std::index_sequence, both of which are relatively easy to implement in C++11.
template<bool...bs>
using bools = std::integer_sequence<bool, bs...>;
template<std::uint64_t x>
using uint64 = std::integral_constant< std::uint64_t, x >;
template<std::size_t N>
constexpr uint64< ((std::uint64_t)1) << (std::uint64_t)N > bit{};
template<std::uint64_t... xs>
struct or_bits : uint64<0> {};
template<std::int64_t x0, std::int64_t... xs>
struct or_bits<x0, xs...> : uint64<x0 | or_bits<xs...>{} > {};
template<bool...bs, std::size_t...Is>
constexpr
uint64<
or_bits<
uint64<
bs?bit<Is>:std::uint64_t(0)
>{}...
>{}
>
from_binary( bools<bs...> bits, std::index_sequence<Is...> ) {
(void)bits; // suppress warning
return {};
}
template<bool...bs>
constexpr
auto from_binary( bools<bs...> bits={} )
-> decltype( from_binary( bits, std::make_index_sequence<sizeof...(bs)>{} ) )
{ return {}; }
It generates the resulting value as a type with a constexpr conversion to scalar. This is slightly more powerful than a constexpr function in its "compile-time-ness".
It assumes that the first bit is the most significant bit in the list.
You can use from_binary<1,0,1>() or from_binary( bools<1,0,1>{} ).
Live example.
This particular style of type-based programming results in code that does all of its work in its signature. The bodies consist of return {};.
I'm using gcc 5.1.0 (c++14) and I was trying with constexpr. Is very annoying to verify if the constexpr I've implemented are evaluated at compile time. I couldn't find any flag for get a warning about that situation.
Here is an example:
example.cpp -----------------------------------------
#include <stdlib.h>
const char pruVar[] = "12345678901234567";
[[gnu::noinline]] constexpr unsigned int myStrlen(const char* cstr)
{
unsigned int i=0;
for(;cstr[i]!=0;++i);
return i;
}
struct CEXAMPLE
{
unsigned int size;
constexpr CEXAMPLE(const char* s): size(myStrlen(s))
{
}
};
int main(void)
{
CEXAMPLE c(pruVar);
unsigned int size = myStrlen(pruVar);
void* a = malloc(c.size + size);
if (a != nullptr)
return 0;
else
return 1;
}
In the example CEXAMPLE::CEXAMPLE is evaluated at compile time including the call to myStrlen in it, but the call to myStrlen in main is being evaluated at runtime. The only way I have to know this is looking at the assembler.This website is very useful too: http://gcc.godbolt.org/
If you know how to make the compiler warn about this or something similar I'll appreciate it
myStrlen(pruVar) can be evaluated at compile time; the compiler is just choosing not to in this instance.
If you want to force the compiler to evaluate it at compile time or error if this is not possible, assign the result to a constexpr variable:
constexpr unsigned int size = myStrlen(pruVar);
^^^^^^^^^
You could also use an enum, or a std::integral_constant:
enum : unsigned int { size = myStrlen(pruVar) };
std::integral_constant<unsigned int, myStrlen(pruVar)> size;
Based on the fact that template arguments must be evaluated at compiletime a helper template can be used.
namespace helper {
template<class T, T v> constexpr T enforce_compiletime() {
constexpr T cv = v;
return cv;
}
}
#define compiletime(arg) ::helper::enforce_compiletime<decltype(arg), (arg)>()
This allows compile time enforcement without an additional constexpr variable, which is handy in order to calculate value lookup tables.
constexpr uint32_t bla(uint8_t blub) {
switch (blub) {
case 5:
return 22;
default:
return 23;
}
}
struct SomeStruct {
uint32_t a;
uint32_t b;
};
SomeStruct aStruct = {compiletime(bla(5)), compiletime(bla(6))};