Precice::span instead of string in an adapter

Hi,
sorry for the probably stupid question… I am working on porting the adapter to preCICE v3 and I am a bit stuck on an initialization like:

int dim = participant->getMeshDimensions(participantName)

where ‘participantName’ for me is a std::string coming from a config file. It complains that:

'getMeshDimensions' is ambiguous '
Candidates are:
int getMeshDimensions(precice::span<const char,unsigned long int-1>)
Semantic Error

How do I correctly initialize precice::span form a std::string?
thank you
Claudio

Hi @Claudio,

This is very interesting. precice::string_view aka precice::span<const char> is constructible from essential everything contiguous you can throw at it. CStrings, arrays, literals, std::string, std::vector, …
The ambiguity is surprising though as it hasn’t come up in testing.

Some questions to clarify this:

  • Which compiler at which version are you using?
  • What is the complete type of the string you are passing to getMeshDimensions()?
  • Can you share the function signature of whatever provides particpantName?

Hi @fsimonis,
thanks for your answer. I am using gcc 11 on an Ubuntu machine with -std=c++17. It turned out that the problem was how I was reading the string from the configuration file (a json file).
I am using something like document["config-file"].GetString() from rapidjson and I was somehow sure that it returned a std::string. It actually returns const Ch*… which Participant or getMeshDimensions don’t like. I made a simple intermediate step like std::string configName = document["config-file"].GetString() and passd configName to Participant to solve everything.
Thanks.
Claudio

Hmm, the compiler shouldn’t be a problem then.

I did some digging and found that:

  • rapidjson uses the Document::Encoding::CharType as a char type.
  • The default encoding for Document is UTF8
  • The default CharType of UTF8 is char

So, this should still return const char*, which is supported by precice::span<const char>.
Hence, the ambiguity seems to be a real issue.

Could you please upload the full error message so that I can try to figure this out? In case it is too sensitive, feel free to mail it to me.
Also, which version of rapidjson are you using?

An interesting step to try is to try to convert the result to const char* like this:

const char* configName = document["config-file"].GetString();

Ok, I didn’t realize that it is the IDE which doesn’t like the initialization: it says:

precice::Participant' is ambiguous '
Candidates are:
 Participant(precice::span<const char,unsigned long int-1>, precice::span<const char,unsigned long int-1>, int, int)
 Participant(precice::span<const char,unsigned long int-1>, precice::span<const char,unsigned long int-1>, int, int, void *)
 Participant(const precice::Participant &)

this happens if I use a direct initialization:

... precice::Participant(document["participantName"].GetString(), ...

or

const char* participantName = document["participantName"].GetString();
... precice::Participant(participantName, ...

but, if I compile it outside the IDE, there is no trace of the issue above. So maybe it is some semantic check that the IDE is not able to resolve, not a real issue.
Anyway

std::string participantName = document["participantName"].GetString();
... precice::Participant(participantName, ...

is good for everyone and I will keep this,
Thanks @fsimonis for your time
Claudio

2 Likes

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.