microsoft / microsoft/STL

<vector>: VS 2019 ignores explicit keyword

Open
#1,041 5 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
C++
Stars
11.2k
Forks
1.7k
Avg merge
4d 15h
Merged PRs (30d)
22

Description

Describe the bug
STL allows range-based insertion to vector (and other containers) thru implicit conversion operator

Command-line test case


d:\Temp2>type repro.cpp
#include <algorithm>
#include <iostream>
#include <vector>

class test
{

};

class no_implicit_convertion
{
public:

        //Here, there is no implicit convertion from one type to another,

        //however, VS2017 ignores explicit keyword.
        explicit no_implicit_convertion(test)
        {
                std::cout << "Inserted test with implicit conversion!" << std::endl;
        }
};

int main()
{
        std::vector<no_implicit_convertion> target_vec;
        std::vector<test> test_vec{ {},{},{} };

        target_vec.insert(target_vec.end(), test_vec.begin(), test_vec.end());
}


d:\Temp2>cl /EHsc /W4 repro.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 19.27.29009.1 for x86
Copyright (C) Microsoft Corporation.  All rights reserved.

repro.cpp
Microsoft (R) Incremental Linker Version 14.27.29009.1
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:repro.exe
repro.obj

d:\Temp2>.\repro.exe
Inserted test with implicit conversion!
Inserted test with implicit conversion!
Inserted test with implicit conversion!

Expected behavior
Should not compile

STL version

Microsoft Visual Studio Professional 2019 Preview
Version 16.7.0 Preview 3.1

Additional context
Also tracked by DevCom-275079 and VSO-636498 / AB#636498

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Begin with the repro.cpp command-line test case and the vector::insert range call using an explicit converting constructor. Compare the observed MSVC 2019 behavior with the expected compilation failure, then identify the STL implementation and regression tests covering range insertion. Done means the test no longer compiles as shown and the relevant test suite passes.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.