samedi 1 août 2015

target_compile_features fails on MinGW-w64 in MSYS2

I recently installed MSYS2 on Windows, along with the MinGW-w64 toolchain and CMake. Specifically, I used the following packages:

  • mingw-w64-i686-gcc
  • mingw-w64-i686-cmake
  • make

Trouble is, whenever I invoke CMake from within the MSYS2 shell with cmake -G"MSYS Makefiles", it fails with the following:

target_compile_features no known features for CXX compiler

"GNU"

version 4.9.2.

The line in CMakeLists.txt that generates the error is this: target_compile_features(myproject PUBLIC cxx_decltype).

If I run CMake from outside the MSYS2 shell (I also have it installed separately) with the "MinGW Makefiles" generator, the makefile generation succeeds.

Inside MSYS2, the CMake version is 3.2.3. The version outside is 3.3.0.

Is there any way to resolve this issue? Thanks in advance.

Differentiate header and c/cpp file

I am trying to find some kind of mechanism on how to tell if you currently are in .h or .cpp file during the compilation time.

Basically the reason is that I need to make some #if #endif based on knowing whether current code is located in header or cpp file.

How to make sure in a constexpr function taking an array that the array is NULL-terminated?

The following code is meant to create sort of a trivial hash of a string up to 8 characters long:

#include <type_traits>
#include <cstdint>
#include <iostream>

template<std::size_t N, std::size_t n=N>
constexpr typename std::enable_if<N<=9 && n==0,
uint64_t>::type string_hash(const char (&)[N])
{
    return 0;
}

template<std::size_t N, std::size_t n=N>
constexpr typename std::enable_if<N<=9 && n!=0,
uint64_t>::type string_hash(const char (&array)[N])
{
    return string_hash<N,n-1>(array) | ((array[n-1]&0xffull)<<(8*(n-1)));
}

For normal string literals and constexpr NULL-terminated strings it does indeed work normally. But if I do something like this:

constexpr char s2[] = {1,2,3,4,5,6,7,8,9};
std::cout << string_hash(s2) << "\n";

, the output will be the same as for the string "\x1\x2\x3\x4\x5\x6\x7\x8". I've tried adding a static_assert(array[N-1]==0,"Failed"); in the definition of string_hash, but the compiler says that array[N-1] is not a constant-expression. I then tried declaring the parameter constexpr, but the compiler said a parameter can't be declared constexpr.

How can I then do this check?

C++11 types for variables defined with auto

I am reading Bjarne Stroustrup's book The C++ Programming Language, and in section 3.2.4, he demonstrates a class hierarchy, starting with an abstract class. His code looks something like this:

class Shape
{
public:
    virtual Point center()const = 0; //Point is a class defined elsewhere and is unimportant for this question
    virtual void move(Point to) = 0;

    virtual void draw()const = 0;
    virtual void rotate(int angle) = 0;
}

class Circle: public Shape
{
    //overrides functions
}

void rotate_all(vector<Shape*>& v, int angle)
{
    for(auto p:v)
        p->rotate(angle);
}

My question is this: what type is the p variable when the function rotate_all is called with a vector of Circle objects? If it was a Shape, then wouldn't the objects be sliced into generic Shape objects? If it was a Circle, then wouldn't the function work only for vectors of Circle objects, and not other subclasses of the Shape class?

How to define metafunctions by undefined types?

Please consider metafunctions like

#include <type_traits>

template <typename T, T N, T M>
struct Sum : std::integral_constant <T, N + M> {};

template <typename T, T N, T M>
struct Product : std::integral_constant <T, N * M> {};

Their result can be extracted through the ::value member:

static_assert (Sum <int, 3, 4>::value == 7, "3 + 4 == 7");
static_assert (Product <int, 2, 5>::value == 10, "2 * 5 == 10");

Both metafunctions have a similar static signature. That is, they associate a T to every pair of T's where T is subject to the same restrictions as those imposed by std::integral_constant and either being summable or multipliable. So we can create a generic metafunction to do the evaluation.

template <typename T, template <typename U, U, U> class F, T N, T M>
struct EvaluateBinaryOperator : std::integral_constant <T, F <T, N, M>::value> {};

static_assert (EvaluateBinaryOperator <int, Sum, 3, 4>::value == 7, "3 + 4 == 7");
static_assert (EvaluateBinaryOperator <int, Product, 2, 5>::value == 10, "2 * 5 == 10");

When used solely in this form, it feels redundant to to pollute Sum and Product with the structure of an std::integral_constant. To show you that we can do without indeed, please consider the following:

template <typename T, T N, T M, T R = N + M>
struct Sum;

template <typename T, T N, T M, T R = N * M>
struct Product;

template <typename> struct EvaluateBinaryOperator;

template <typename T, template <typename U, U, U, U> class F, T N, T M, T R>
struct EvaluateBinaryOperator <F <T, N, M, R> > : std::integral_constant <T, R> {};

static_assert (EvaluateBinaryOperator <Sum <int, 3, 4> >::value == 7, "3 + 4 == 7");
static_assert (EvaluateBinaryOperator <Product <int, 2, 5> >::value == 10, "2 * 5 == 10");

Instead of using members of Sum and Product, we specialize on a default argument and extract it only in EvaluateBinaryOperator. As an added bonus, Sum and Product can remain without definition, rendering them trivially non-inferrable and non-constructable and the syntax looks much cleaner too. Now, here's the catch. What if we would like all our metafunctions to have a uniform static interface? That is, what if we introduce

template <typename...> struct Tuple;

template <typename T, T> struct Value;

and require all our metafunctions to look like template <typename> struct? For instance,

template <typename> struct Sum;

template <typename T, T N, T M>
struct Sum <Tuple <Value <T, N>, Value <T, M> > > : 
    std::integral_constant <T, N + M> {};

template <typename> struct Product;

template <typename T, T N, T M>
struct Product <Tuple <Value <T, N>, Value <T, M> > > : 
    std::integral_constant <T, N * M> {};

Now, we would like to transform them to something like:

template <typename, typename> struct Sum;

template <typename T, T N, T M, typename R = Tuple <Value <T, N + M> > >
struct Sum <Tuple <Value <T, N>, Value <T, M> >, R>;

template <typename, typename> struct Product;

template <typename T, T N, T M, typename R = Tuple <Value <T, N * M> > >
struct Product <Tuple <Value <T, N>, Value <T, M> >, R>;

Such that we can extract values with

template <typename> struct Evaluate;

template <template <typename, typename> class F, typename I, typename O>
struct Evaluate <F <I, O> > {
    typedef O Type;
};

static_assert (std::is_same <
    Evaluate <Sum <Tuple <Value <int, 3>, Value <int, 4> > > >::Type
    Tuple <Value <int, 7>
>, "3 + 4 == 7");
static_assert (std::is_same <
    Evaluate <Product <Tuple <Value <int, 2>, Value <int, 5> > > >::Type
    Tuple <Value <int, 10>
>, "2 * 5 == 10");

Those of you familiar with the C++ standard will immediately point to 14.5.5/8: "The template parameter list of a specialization shall not contain default template argument values.", accompanied by the teasing footnote: "There is no way in which they could be used.". Indeed, feeding just about any modern compiler this code yields a compiler error on the Sum and Product template specializations about violation of the standard. Apart from proving the aforementioned footnote to lack the imagination of the author; we've created ourselves a valid use case for them.

My question can now be put: Are there any other ways to achieve a similar effect where Sum and Product remain undefined / incomplete types, thereby trivially being non-inferrable and non-constructable, while still carrying responsibility for performing the operation? Any suggestions are welcome. Thanks in advance.

c++ giving wrong answer in codechef

I am getting wrong answer for the given problem. But it is running properly in cpp.sh http://ift.tt/1ezaDoz

my solution is:-

// Example program


#include <iostream>


using namespace std;

int prime(int a)

{

    //cout<<"****check prime****";

    //if(a==0){return ;}

    int j;

    for(j=2;j<=a/2;j++)

    {if((a%j)==0)

{//cout<<endl<<"***   ****"<<j<<endl;

   return j;}

    }

    //cout<<endl<<"***  -1 ****"<<endl;

    return -1;
}


int main()

{

    int t;

    cin>>t;

    int a[t];

    int n;int x=0;

    for(int i=0;i<t;i++)

    {

        cin>>n;

        x=prime(n);

        if(x==-1){a[i]=-1; }

        else{a[i]=0;}

        }

for(int i=0;i<t;i++)

{


    if(a[i]==-1){cout<<"LUCKY NUMBER"<<endl;}

    else{cout<<"sorry"<<endl;}

}   

return 0;
}

Pass a parameter pack to another parameter packed function

I am trying to call a function with this signature:

template <class Component, class ... Args>
void addComponent(const Entity & ent, Args&& ... args){

Using this function:

template <class Component, class ... Args>
void Entity::add(Args&& ... args){
    manager->addComponent<Component>(*this, std::forward<Args>(args)...);
}

However, VS2015 is complaining that it is "unable to match function definition to an existing declaration". What is the problem here?