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 8879881
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:03:10+00:00 2026-06-14T20:03:10+00:00

I have to build a small OpenGL wrapper for a work. I’m trying to

  • 0

I have to build a small OpenGL wrapper for a work. I’m trying to avoid writing copy constructors and copy assignment for all my classes.

The one way to be really lazy and never write copy is to use pointers, but since pointers are evil I’m trying to use exclusively std::shared_ptr.

Problem is by using a constructor that receives a std::shared_ptr by value my program crashes, and when using perfect forwarding it only works when I’m passing an lvalue.

// this class doesn't have any default, copy constructors.
class Dep
{
    Dep(std::string path, GLenum type);
};

class Program
{
std::shared_ptr<Dep> dep1;
std::shared_ptr<Dep> dep2;

(...)

I have tried 2 different kinds of constructors:

template <class T, class = typename std::enable_if<std::is_constructible<std::shared_ptr<Dep>, T>::value>::type>
Program(T&& dep1, T&& dep2)
: dep1(std::forward<T>(dep1)), dep2(std::forward<T>(dep2))
{
}

and the other one

Program(std::shared_ptr<Dep> dep1, std::shared_ptr<Dep> dep2)
: dep1(std::move(dep1)), dep2(std::move(dep2))
{
}

What I want to do is being able to pass pass either an lvalue or rvalue shared pointer, but it doesn’t work it crashes every time unless I use lvalue on the forward one.

// passing these work on the std::forward one, but that's the only case it works
// if i try to use std::make_shared as parameter (for rvalue) it crashes on both
// the std::move and std::forward ones.
auto vs = std::make_shared<GLShader>("TriangleVS.glsl", GL_VERTEX_SHADER);
auto fs = std::make_shared<GLShader>("TriangleFS.glsl", GL_FRAGMENT_SHADER);

summary: lvalue on the std::forward one works. rvalue on std::forward does not work. lvalue or rvalue on the std::move one dont work. it just hangs the program when the std::shared_ptr constructor is called (inside the Program constructor).

I watched Scott Mayers universal references talk and I thought I was understanding this, and this happens to me.

  • 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-14T20:03:12+00:00Added an answer on June 14, 2026 at 8:03 pm

    I don’t see anything wrong with this code, and it tests out OK on http://ideone.com/jlShgB too:

    #include <memory>
    #include <utility>
    #include <string>
    #include <cassert>
    
    enum GLenum { foo };
    
    // this class doesn't have any default, copy constructors.
    struct Dep
    {
        Dep(std::string path, GLenum type) {}
        Dep() = delete;
        Dep(Dep const&) = delete;
    };
    
    struct Program
    {
        std::shared_ptr<Dep> dep1;
        std::shared_ptr<Dep> dep2;
    
    #if 1
        template <class T, class = typename std::enable_if<std::is_constructible<std::shared_ptr<Dep>, T>::value>::type>
        Program(T&& dep1, T&& dep2)
            : dep1(std::forward<T>(dep1)), dep2(std::forward<T>(dep2))
        {
        }
    #else
        Program(std::shared_ptr<Dep> dep1, std::shared_ptr<Dep> dep2)
            : dep1(std::move(dep1)), dep2(std::move(dep2))
        {
        }
    #endif
    };
    
    int main()
    {
        auto dep1 = std::make_shared<Dep>("dep1", foo);
        auto dep2 = std::make_shared<Dep>("dep2", foo);
        Program p(std::move(dep1), std::move(dep2));
    
        assert(!dep1 && !dep2);
    }
    

    Of course if you change #if 1 to #if 0, the assert will raise an exception because the dep1/dep2 will not have been moved from.

    This leads me to suspect another issue somewhere else. If you can isolate a SSCCE that exhibits the problem, please let me know.

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

Sidebar

Related Questions

i have build a small function whose work to get value in alert box
I'm trying to build a small testing app with erlang+mnesia. I have a user
I am trying to build a small application using knockout, require, underscore. I have
i have build a small function whose work is to change banner image path
I'm trying to build a small wiki, but I'm having problems writing the regex
i have build a small test app in Flash Pro 5.5 overlayed with the
I have been using twitter bootstrap to build a small blog site for myself
I have a small problem with UINavigationController animation between two view. My application build
I have tried to build a small function that would be use in controller
I have build.xml for my project, but even this small piece of code <target

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.