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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T08:14:20+00:00 2026-06-05T08:14:20+00:00

Suppose I have a class like this: class Foo { std::vector<int> bar; public: std::vector<int>&

  • 0

Suppose I have a class like this:

class Foo
{
  std::vector<int> bar;

public:
  std::vector<int>& get_bar() { return bar; }
};

and later on, I want another variable somewhere else that has the same type as bar. It would make sense to me if I could just do this:

decltype(Foo::bar) clone_of_bar;

But that doesn’t work. The compiler tells me ‘std::vector< int > Foo::bar’ is private.

So I end up having to use something like this:

std::remove_reference<decltype(std::declval<Foo>().get_bar())>::type clone_of_bar;

Which works, but looks like a complete mess. Maybe there’s an easier way to do it; I’m not really sure. But what I really want to know is why I can’t just use decltype(Foo::bar). Why should anyone care that bar is private? It’s not like I’m actually accessing the variable.

decltype is a new feature of the language. I just don’t understand why it was designed to not work on private variables.

  • 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-05T08:14:21+00:00Added an answer on June 5, 2026 at 8:14 am

    In language lawyer terms, bar is a name, to use it in the decltype expression the compiler has to do normal name lookup, which respects access control.
    Why should decltype have been designed differently to the rest of the language? You haven’t presented any convincing argument for why it shouldn’t be consistent with e.g. sizeof.

    As a class author, I don’t want you to be able to query private implementation details like that. If I wanted the type to be usable outside the class I’d define a public typedef telling you what type it is.

    and later on, I want another variable somewhere else that has the same type as bar

    You want “another variable” that’s the same type as a private implementation detail? So if the author of class Foo refactors their code and replaces the type with some other implementation detail, suddenly your code changes meaning and unrelated code might suddenly stop compiling or silently have different behaviour, because that code foolishly relied on private details that were none of its business. That would introduce coupling between private implementation details and unrelated code that the author of Foo might not even know exists! That’s a terrible idea.

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

Sidebar

Related Questions

Suppose I have some code like this: class Base { public: virtual int Foo(int)
Suppose I have this class: class foo { public: foo() { } foo(const std::string&
Suppose you have a simple class like this: class foo{ private: int* mData; int
Suppose I have something like this: class A { public B mem; public int
For example, suppose I have a class: class Foo { public: std::string& Name() {
Suppose I have class like this (simplified): class Foo_p; class Foo { private: Foo_p
Suppose I have a class public class Foo { public Bar Bar { get;
Suppose I have the following class: public class Foo { public Bar bar; }
Suppose I have code like this: template<class T, T initial_t> class Bar { //
Suppose I have a Java class like this: public class Test { static {

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.