-1

EDIT: the source of the error was a typo. Apologies for the confusion, but thanks to the many people who contributed valuable peripheral advise in comments.

The below code

#include <iostream>
#include <string>
#include <cmath>
#include <math.h>
#include <cfloat>
#include <Rcpp.h>
#include <cassert>
using namespace Rcpp;

int test(){
  std::string s(2,"");
  return 1;
}

induces the following IDE error:

no matching constructor for initialization of 'std::string'

Running the code in R generates an inexpressive seg fault, but I hope the above is sufficient. I see that in this discussion the consensus is that this may happen when the used clang version is outmoded (clang++3.7 being the passing build). Having referred to this discussion and seeing that my machine (macos) is running the following clang:

Apple clang version 11.0.3 (clang-1103.0.32.59)

I'm not sure that the issue I'm running into is a duplicate of the one featured in the above discussion.

If I had to guess, the source of the issue is likely the compiler version, something about rcpp, or a library conflict.

EDIT: here and here are the sources which led me to believe that std ships with a default string constructor (see fill (6) for latter)

Josh Purtell
  • 334
  • 2
  • 10
  • 1
    What are you trying to do with your string constructor? And which of the many overloads do you think should match the (`int`, `const char*`) argument list? – Adrian Mole Sep 08 '20 at 16:21
  • 1
    As an aside, you don't need all the extra include statements as including `Rcpp.h` will pull these in---it certainly does so for `string`. – Dirk Eddelbuettel Sep 08 '20 at 16:32
  • Vote to lose as non-reproducable / typo, as OP got confused about `""` and `''` (see answer below). – DevSolar Sep 08 '20 at 16:33

1 Answers1

1

Well?

  std::string s(2,"");

no matching constructor for initialization of 'std::string'

Exactly what it says in the error message, std::string does not have a constructor that matches your int, char const * arguments.

If I had to guess, the source of the issue is likely the compiler version, something about rcpp, or a library conflict.

No, the issue is that there is no constructor matching your initialization attempt of std::string. If you had attempted to reduce the example even further -- i.e., removing all includes other than the one you are actually using (<string>), that might have become apparent.

DevSolar
  • 67,862
  • 21
  • 134
  • 209
  • Hi DevSolar, thanks for the quick answer. I edited the answer with sources to which I had referred when surmising that there exists such a constructor – Josh Purtell Sep 08 '20 at 16:28
  • @JoshPurtell: That constructor variant ou pointed out takes a *character* as second argument, not a string literal. That would then have to be `s(2, '\0')` (which is a questionable choice for an initialization; why would you want that?). -- Sidenote: cppreference.com is generally considered to be a higher quality resource than cplusplus.com, which has somewhat of a history of being just slightly wrong here and there... – DevSolar Sep 08 '20 at 16:29
  • Ok, perfect. It's been a while since I've used c++ and the difference between ' and '' slipped my mind. I assume (?) that it would be best for me to delete the answer, but if you prefer I heavily edit it to reflect the basic nature of the error so that your answer is not lost, let me know. – Josh Purtell Sep 08 '20 at 16:31
  • @JoshPurtell: Nah, it's good. I'll vote for close as "typo", and that will probably be it. You shouldn't delete questions that are already answered. The SO engine frowns on that. – DevSolar Sep 08 '20 at 16:32
  • Ok, makes sense, thanks – Josh Purtell Sep 08 '20 at 16:35
  • The reason I wanted to initialize with a certain char is that initializing just using char s[2] generated undefined behavior in my code, and I wanted to see if there was some subtle memory issue at play. It turns out I was correct(?) and the undefined behavior ceased once I used the above initialization. FWIW – Josh Purtell Sep 08 '20 at 16:43
  • 1
    @JoshPurtell: Uh... uh... C++ is very much **not** a "trial & error" language... just because it stopped blowing up in your face doesn't mean that UB went away, and / or won't blow up in your face at some later point. I seriously doubt that something that got "fixed" by an initialization like that is correct code. Perhaps post another question on that... – DevSolar Sep 08 '20 at 17:30