4

I have this struct:

struct MyStruct
{
    MyStruct(const Wrapper &wrapper, /*...*/) :
        wrapper(std::cref(wrapper)), /*...*/ {}

    std::reference_wrapper<Wrapper const> wrapper;
    /*...*/
};

However, now I want to use a tbb::concurrent_bounded_queue<MyStruct>, which is not possible since MyStruct doesn't have a default constructor. But I don't know how to define it since wrapper must be initialized somehow. Is there any solution for this (apart from using raw pointers)?

tbb error (where FindAffineShapeArgs is MyStruct here):

/usr/include/tbb/concurrent_queue.h(453): error: no default constructor exists for class "FindAffineShapeArgs"
          T value;
            ^
          detected during:
            instantiation of "void tbb::concurrent_bounded_queue<T, A>::clear() [with T=FindAffineShapeArgs, A=tbb::cache_aligned_allocator<FindAffineShapeArgs>]" at line 446
            instantiation of "tbb::concurrent_bounded_queue<T, A>::~concurrent_bounded_queue() [with T=FindAffineShapeArgs, A=tbb::cache_aligned_allocator<FindAffineShapeArgs>]" at line 272 of "/home/luca/Dropbox/HKUST/CloudCache/cloudcache/CloudCache/Descriptors/hesaff/pyramid.cpp"
justHelloWorld
  • 6,478
  • 8
  • 58
  • 138
  • Why do you need that? Can't you just delete your default constructor? – πάντα ῥεῖ May 25 '17 at 14:13
  • I'm not really familiar with `tbb::concurrent_bounded_queue`, but a casual reading of the documentation doesn't suggest that it would require its elements to be default-constructible. What makes you believe the lack of such a constructor would pose a problem? – Igor Tandetnik May 25 '17 at 14:14
  • 4
    If you want to have a reference you cannot be default constructable. You can use a pointer but then you need to check for null values all over the place. Pick you poison. – NathanOliver May 25 '17 at 14:14
  • @Raxvan That would exhibit undefined behavior. – Igor Tandetnik May 25 '17 at 14:16
  • @IgorTandetnik Look at my updated answer (using `icpc` as compiler). Did I misunderstood this error? – justHelloWorld May 25 '17 at 14:18
  • @πάνταῥεῖ I don't have a default constructor – justHelloWorld May 25 '17 at 14:19
  • 1
    Just use a pointer. KISS. Stop banging your head against the wrapper. – John Zwinck May 25 '17 at 14:20
  • I see. `tbb::concurrent_bounded_queue` seems to be rather poorly written, when something as innocuous as `clear()` inexplicably wants to default-construct an element. – Igor Tandetnik May 25 '17 at 14:21
  • @justHelloWorld _"I don't have a default constructor"_ Does that `tbb` stuff require you to provide one? – πάντα ῥεῖ May 25 '17 at 14:21
  • @πάνταῥεῖ it seems so from my updated question – justHelloWorld May 25 '17 at 14:26
  • @IgorTandetnik An older version I found on the interwebs default-constructs an element per element in the queue :( – T.C. May 25 '17 at 17:57
  • 2
    @IgorTandetnik Anyway, `concurrent_bounded_queue` says it's "similiar to `concurrent_queue`", whose [documentation](https://software.intel.com/en-us/node/506200) does call out that it requires default constructible elements. Still, badly organized. – T.C. May 25 '17 at 18:04

0 Answers0