Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7735211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T07:26:38+00:00 2026-06-01T07:26:38+00:00

I have a much simpler set of requirements and do not need much of

  • 0

I have a much simpler set of requirements and do not need much of variant’s machinery. I also do not want to depend on boost if I can help it.

I need to store either an arbitrary type known at compile time (that may be void). It is either move constructable or copy constructable, and if either of those throw, it is permissible for the contained value to be undefined.

Instead of this value, it may also either contain an ::std::exception_ptr or an ::std::error_code.

::boost::variant<T, ::std::exception_ptr, ::std::error_code> would work if T is allowed to be void. Except that ::boost::variant provides the ‘never-empty guarantee’ which I don’t actually need in this instance. And, if I understand how it works correctly, it’s not very compatible with types that can be moved but not copied.

Right now I’m writing a lot of duplicated code that I shouldn’t have to be writing to handle each of these types separately. I’m also storing a copy of an object of each type and flag values saying which is relevant. Lastly, void gives the whole system conniptions and is requiring I write specializations all over the place.

Is there a better way?

Here’s a simplified example of what I have. This is basically a class designed to hold a result for transmission to another thread, sort of like a future:

template <typename ResultType>
class stored_result {
 public:
   stored_result() : is_val_(false), is_err_(false), is_exception_(false) { }

   void set_bad_result(::std::error err) {
      is_err_ = true;
      error_ = ::std::move(err);
   }
   void set_bad_result(::std::exception_ptr exception) {
      is_exception_ = true;
      exception_ = ::std::move(exception);
   }
   void set_result(ResultType res) {
      is_val_ = true;
      val_ = ::std::move(res);
   }

   ResultType result() {
      if (is_val_) {
         is_val_ = false;
         return ::std::move(val_);
      } else if (is_exception_) {
         is_exception_ = false;
         ::std::rethrow_exception(::std::move(exception_));
      } else if (is_error_) {
         is_error_ = false;
         throw ::std::system_error(error_);
      } else {
         throw ::std::runtime_error("Asked for result when there was none.");
      }
   }

 private:
   bool is_val_, is_err_, is_exception_;
   T val_;
   ::std::exception_ptr exception_;
   ::std::error_code error_;
};
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T07:26:39+00:00Added an answer on June 1, 2026 at 7:26 am

    An example of how to handle void in a transparent manner with the code you have right now:

    struct empty_type {};
    
    template<typename T>
    using Store = typename std::conditional<std::is_void<T>::value, empty_type, T>::type;
    
    template<typename T>
    T&&
    restore(T&& t)
    {
        return std::forward<T>(t);
    }
    
    void
    restore(empty_type)
    {}
    
    template <typename ResultType>
    class stored_result {
    public:
        // snipped everything that is left unchanged
    
        template<
            typename U = ResultType
            , typename std::enable_if<
                !std::is_void<U>::value
                , int
            >::type = 0
        >          
        void set_result(U res) {
            is_val_ = true;
            val_ = std::move(res);
        }
    
        template<
            typename U = ResultType
            , typename std::enable_if<
                std::is_void<U>::value
                , int
            >::type = 0
        >          
        void set_result() {
            is_val_ = true;
        }
    
        ResultType result() {
            if (is_val_) {
                is_val_ = false;
                return restore(std::move(val_));
            } else if {
                // rest as before
        }
    
    private:
        Store<T> val_;
    };
    

    Although the code is untested may have some kinks.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Made a much simpler example, hopefully someone can follow this and help me Here
I'm currently extending a Site and do not have much experience with symfony. Currently
Need a list of Pcap.Net members or classes? Their website doesn't have much documentation
This might be a simple one, but since I don't have much knowledge about
I don't have much experience working with resultsets, but as ResultSet is an interface,
I don't have much experience with Serial I/O, but have recently been tasked with
This post on SO answers most of the questions I have (much thanks to
What does x :: xs' mean? I dont have much functional experience but IIRC
O.K., I'm coming from Perl to Python and I don't have much experience in
As another post I've made noted, I don't have much experience with jQuery, and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.