Simplified/Extended syntax to import names onto current scope

66 views
Skip to first unread message

iphone.jav...@gmail.com

unread,
Apr 30, 2018, 10:20:40 AM4/30/18
to ISO C++ Standard - Future Proposals
Importing a name onto the current scope provides the following syntax:

#include <string>
#include <vector>

using std::string;
using std::vector;

or, alternatively:

using namespace std;

On one hand, you get selective (one at a time) control or an all-or-nothing.  Consider a more complicated program (this is reality for many). Pick all of the following in one line or 10 lines:

#include <string>
#include <vector>
#include <tuple>
#include <utility>
#include <memory>

using std::string;
using std::vector;

using std::tuple;
using std::make_tuple;

using std::pair;
using std::make_pair;

using std::shared_ptr;
using std::make_shared;

using std::unique_ptr;
using std::make_unique;


You get the idea. That's a lot of fluff before you get to the crux of the program.

* * *

This proposal is for the following simplified syntax (two alternatives).


1. Allow the use of a comma to separate each imported name.  This is just a simplified version that eliminates the multiple using statements, allowing to separate the imported names with a comma.

#include <string>
#include <vector>
#include <tuple> 
 
using std::vector, std::string, std::tuple, std::make_tuple;

or using different namespaces:

#include <string>
#include <vector>
#include <tuple> 

#include <telephony.hpp> // declares multiple types within a telephony namespace 
 
using std::vector, std::string, std::tuple, std::make_tuple, telephony::provider, telephony::address; 

2. Enclose the imported names in curly braces.

#include <vector>
#include <string>
#include <tuple>
#include <iostream>
#include <chrono> 

#include <telephony.hpp> // declares multiple types within a telephony namespace

using std { vector, string, tuple, make_tuple, cout, endl };
using std::chrono { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday }; 
using telephony { provider, address, terminal, connection, terminal_connection };



Comments are welcome.


Ville Voutilainen

unread,
Apr 30, 2018, 10:24:06 AM4/30/18
to ISO C++ Standard - Future Proposals
On 30 April 2018 at 17:20, <iphone.jav...@gmail.com> wrote:
> 1. Allow the use of a comma to separate each imported name. This is just a
> simplified version that eliminates the multiple using statements, allowing
> to separate the imported names with a comma.
>
> #include <string>
> #include <vector>
> #include <tuple>
>
>
>
> using std::vector, std::string, std::tuple, std::make_tuple;

This program is valid C++17, so what you're asking for is already there.

Barry Revzin

unread,
Apr 30, 2018, 10:46:54 AM4/30/18
to ISO C++ Standard - Future Proposals

Nicol Bolas

unread,
Apr 30, 2018, 12:00:10 PM4/30/18
to ISO C++ Standard - Future Proposals, iphone.jav...@gmail.com
Despite the fact that, as Ville pointed out, we can already do one of those, I don't like the idea of putting all of those on different lines. It makes it really hard to figure out what has been imported into the current namespace and what has not.

It would be better to just stick `std::` in front of those names when you use them. After all, you can't put `using std::vector` in a header (it leaks to anything that includes it), so anytime a header needs to use `std::vector`, that's how you have to spell it. Since a lot of code gets put into headers, you're going to see `std::vector` and so forth a lot.

iphone.jav...@gmail.com

unread,
Apr 30, 2018, 12:12:18 PM4/30/18
to ISO C++ Standard - Future Proposals
Hi, Ville,

You are correct for the first syntax.

Nicol Bolas

unread,
Apr 30, 2018, 2:00:12 PM4/30/18
to ISO C++ Standard - Future Proposals, iphone.jav...@gmail.com
On Monday, April 30, 2018 at 12:00:10 PM UTC-4, Nicol Bolas wrote:
Despite the fact that, as Ville pointed out, we can already do one of those, I don't like the idea of putting all of those on different lines. It makes it really hard to figure out what has been imported into the current namespace and what has not.

It would be better to just stick `std::` in front of those names when you use them. After all, you can't put `using std::vector` in a header (it leaks to anything that includes it), so anytime a header needs to use `std::vector`, that's how you have to spell it. Since a lot of code gets put into headers, you're going to see `std::vector` and so forth a lot.

Two things. First, I meant "on the same lines" not "on different lines".

Second, with the advent of modules, we may be heading towards a future where `using identifier;` starts becoming much more commonplace. We avoid `using` declarations in headers only because we need to avoid leaking. And since all template code has to be in headers, lots of C++ gets written there. Since we're already used to `std::vector` in our headers, it often isn't worth the effort to put `using std::vector` in our source files.

But with modules, leaking isn't possible, so `using` declarations are far more reasonable. In such a system, it's now much more reasonable to have some preamble that invokes `using` for a bunch of names that will be commonly used. Even `using namespace *` might start being reasonable in such circumstances.

Given that, a way to shorten that preamble in terms of length might be appropriate. Of course, we already have that in C++17, so... there we are ;)
Reply all
Reply to author
Forward
0 new messages