2

I am reading a wav file,and pushing the data in a std::array in the end. I need to make some operation on the chunks of data. So I thought this is a good opportunity to learn Eric Niebler's ranges.

I saw view_facade in manual page under "custom ranges" section but than I saw this question: link . Now I am not sure how to make a custom range class. Can anybody help me about that? The code below shows what I am trying to achieve.

#include <iostream>
#include <range/v3/all.hpp>

using namespace ranges;
using namespace std;


struct A
{
    static constexpr size_t MAX_SIZE = 100000;


    A ()
    {
        for ( size_t i = 0; i < MAX_SIZE; i++)
            data[i] = i;
        size = MAX_SIZE;
    }

    auto begin() const { return data.begin(); }
    auto end() const { return data.end(); }


    std::array< double , MAX_SIZE > data;
    size_t size;

};

int main()
{
    A instance;
    RANGES_FOR(auto chunk, view::all(instance) | view::chunk(256)) {

    }
    return 0;
}

Part of compile output:

14:47:23: Running steps for project tryOuts...
14:47:23: Configuration unchanged, skipping qmake step.
14:47:23: Starting: "C:\Qt\Tools\mingw491_32\bin\mingw32-make.exe" 
C:/Qt/Tools/mingw491_32/bin/mingw32-make -f Makefile.Debug
mingw32-make[1]: Entering directory 'C:/Users/Erdem/Documents/build-tryOuts-Desktop_Qt_5_4_2_MinGW_32bit-Debug'
g++ -c -pipe -fno-keep-inline-dllexport -std=gnu++1y -pthread -lpthread -O3 -g -frtti -Wall -Wextra -fexceptions -mthreads -DUNICODE -I"..\tryOuts" -I"." -I"..\..\..\..\range-v3-master\include" -I"D:\cvOutNoIPP\install\include" -I"..\..\..\..\Qt\5.4\mingw491_32\mkspecs\win32-g++"  -o debug\main.o ..\tryOuts\main.cpp
In file included from ..\..\..\..\range-v3-master\include/range/v3/utility/iterator.hpp:28:0,
                 from ..\..\..\..\range-v3-master\include/range/v3/begin_end.hpp:24,
                 from ..\..\..\..\range-v3-master\include/range/v3/core.hpp:17,
                 from ..\..\..\..\range-v3-master\include/range/v3/all.hpp:17,
                 from ..\tryOuts\main.cpp:2:
..\..\..\..\range-v3-master\include/range/v3/utility/basic_iterator.hpp:445:22: error: 'constexpr const T& ranges::v3::basic_mixin<Cur>::get() const' cannot be overloaded
             T const &get() const noexcept
                      ^

------------ Update -------------------------------------------

If I add CONFIG += c++14 the code almost compiles except auto return type deduction errors below :

main.cpp:22: deduced return type only available with -std=c++1y or -std=gnu++1y

to avoid those errors I am using CONFIG += c++1y.But in this case I am getting bunch of errors that I post in first place. I know from D language so called "Voldemort Types" are important, I don't want to give up return type deduction. Which flag in gcc should I use?

Community
  • 1
  • 1
Kadir Erdem Demir
  • 3,531
  • 3
  • 28
  • 39

1 Answers1

1

I'm still learning the range library myself, but my understanding is that things that expose STL-compatible begin() and end() methods can be used as a view. So for example, with your Reader class you could have

struct Reader {
    // ... 

    auto begin() const { return rawData.begin(); }

    auto end() const { return rawData.end(); }

};

You could then use view::all() to create a view around the Reader, something like

Reader r;
RANGES_FOR(auto chunk, view::all(r) | view::chunk(256)) {
    ...
}

As I say, I'm still learning the library myself, but hopefully this will help.

Tristan Brindle
  • 16,281
  • 4
  • 39
  • 82
  • Unfortunately not works, a small piece from one of the error messages: error: 'constexpr const T& ranges::v3::basic_mixin::get() const' cannot be overloaded T const &get() const noexcept . – Kadir Erdem Demir Nov 20 '15 at 04:05
  • Tristan's answer is correct. I don't understand the answer you got. Perhaps you could post your whole code. – Eric Niebler Nov 20 '15 at 20:08
  • I edited my question. I simplified the example in to a single file . I hope now it is more understandable. And I copied part of compile output. – Kadir Erdem Demir Nov 21 '15 at 12:53
  • @EricNiebler I think the problem is with mingw flags if I use CONFIG += c++14 than I am only getting return type reduction errors. I know from D language so called "Voldemort Types" are important, I don't want to give up on return type deduction. Do you have any idea which flag in mingw I should use? – Kadir Erdem Demir Nov 22 '15 at 14:26
  • Your code compiles for me with -std=c++1y. I don't know what build tool you're using. I use CMake, so I use "`CMAKE_CXX_FLAGS=-std=c++1y`". HTH. – Eric Niebler Dec 15 '15 at 18:16