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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:50:18+00:00 2026-06-17T07:50:18+00:00

I have been looking at creating a synchroniser helper template class which is based

  • 0

I have been looking at creating a synchroniser helper template class which is based on Herb Sutter’s ideas of a wrapper class in this talk This does not work in msvc as is (unless we remove the brace initialisation) but when brace initialisation is removed then it’s fine.

In clang/gcc (ubuntu 12.10, gcc4.7.2, clang (3.2) self built with libc++) it seems the private access modifier has to appear before the public: which seems a little strange.

The error in gcc is
error: ‘t_’ was not declared in this scope

and clang is

error: use of undeclared identifier 't_'
  auto operator()(F f) const ->decltype(f(t_))

It may be a template/declytpe issue that I am not aware of and wonder if anyone can help with this one. (all compiled with relevant c++11 flags)

template <class T>
class Synchronised {
    public:
        Synchronised(T t = T{}) : t_{t} {}
        template <typename F>
        auto operator()(F f) const -> decltype(f(t_)) {
            std::lock_guard<std::mutex> lock{mutex_};
            return f(t_);
        }
        private: // place this before public: and this object compiles
            mutable T t_;
            mutable std::mutex mutex_;
};

Edit: Adding Johannes’s ideas and full class in case anyone wants a cut and paste.

#include <future>
#include <iostream>
#include <thread>
#include <vector>

template <class T> T &self(T &t) { return t;  }
template<typename T> struct Dependent {  };

template<typename T>
class Synchronised : Dependent<T>{
 public:
  explicit Synchronised(T t = T()) : t_(t) {}
  template<typename Functor>
  auto operator()(Functor functor) const ->decltype(functor(self(*this).t_)) {
  //auto operator()(Functor functor) const ->decltype(functor(this->t_)) {
    std::lock_guard<std::mutex> lock(mutex_);
    return functor(t_);
  }
 private:
  mutable T t_;
  mutable std::mutex mutex_;
};


int main() {

    Synchronised<std::string> sync_string("Start\n");
    std::vector<std::future<void>> futures;
}
  • 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-17T07:50:19+00:00Added an answer on June 17, 2026 at 7:50 am

    The below was only sufficient for making the class template definition itself valid. However the same rules that made the lookup not find the data member in the class template (which necessiated the introduction of the empty dependent base class or the dependent function call) will also make the instantiation of the class template not find the data member, and thereby will trigger a compiler error.

    We told the compiler “hold on, perhaps you will find the data member at instantiation time”, but I did not think about what will happen when actually instantiating. We will now make it so that the name is still dependent even after instantiation of the class happened. The resolution will have to wait until the call to operator().

    // keep this little util somewhere :)
    template<typename T>
    struct self { 
      template<typename U> U &operator()(U &t) { return t; } 
    };
    
    template <class T>
    class Synchronised {
        public:
    // ...
            auto operator()(F f) const -> decltype(f(self<F>()(*this).t_)) {
    // ...
    };
    

    The use of a class template for self instead of a function template will also prevent argument dependent lookup from happening, preventing that the author of F also writes a function called self that matches the argument *this (this could have been a potential problem with the partial solution below, too).


    You have several other options, beside reordering

    1. Making the expression on the left side of . dependent, but not just the enclosing class (because it will be special-cased)

      // keep this little util somewhere :)
      template <class T> T &self(T &t) { return t; }
      
      template <class T>
      class Synchronised {
          public:
      // ...
              auto operator()(F f) const -> decltype(f(self(*this).t_)) {
      // ...
      };
      
    2. Introduce a dependent base class to work-around the special casing of the enclosing class

      // Keep this little util somewhere
      template<typename T> struct Dependent { };
      
      template <class T>
      class Synchronised : Dependent<T> {
          public:
      // ...
              auto operator()(F f) const -> decltype(f(this->t_)) {
      // ...
      };
      

    The first is based on the Standard making self(*this).t_ a member of an unknown specialization

    • the type of the object expression is dependent and is not the current instantiation.

    The second is based on the Standard making this->t_ a member of an unknown specialization

    • the type of the object expression is the current instantiation, the current instantiation has at least one dependent base class, and name lookup of the id-expression does not find a member of the current instantiation or a non-dependent base class thereof;

    This in turn makes x->t_ for both cases a dependent expression and hence the name will be looked up at instantiation time. The Standard says

    A class member access expression (5.2.5) is type-dependent if the expression refers to a member of the current instantiation and the type of the referenced member is dependent, or the class member access expression refers to a member of an unknown specialization.

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

Sidebar

Related Questions

I have been looking into different systems for creating a fast cache in a
I have been looking for an answer to this on Stack Overflow, but I
I have been looking through the source code for the Flash-based Google TV example
I have been looking at this for to long so I am tossing it
I have been looking into the possibility of creating a soft copy(image/EMF file) of
I have been looking at creating an augmented reality application. Can anyone suggest a
I have been looking at this for a while, and am currently at a
I have been looking at this article about using tasks in C#, and I
I have been looking around and found no solution for this issue. Let say
I have been looking into some options for creating a slide out bar for

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.