1

I want something like this:

if (customLocation.isEmpty())
{
    KUrl url;
}
else
{
    KUrl url(customLocation);
}
/* use url */
masoud
  • 55,379
  • 16
  • 141
  • 208
basin
  • 3,949
  • 2
  • 27
  • 63

5 Answers5

1

Any reason you can't do

KUrl url;
if (!customLocation.isEmpty())
{
    url = KUrl(customLocation);
}
/* use url */

or

KUrl url = customLocation.isEmpty() ? KUrl() : KUrl(customLocation);
Jack Aidley
  • 19,439
  • 7
  • 43
  • 70
  • 1
    Then there's redundant constructor call – basin Apr 06 '13 at 12:40
  • @user447503 What do you mean by redundant, what's your concern? – john Apr 06 '13 at 12:41
  • @john First the default constructor is called, then the overloaded. – basin Apr 06 '13 at 12:43
  • Yes I know that, but *why* is it a concern? The code will still work no? – john Apr 06 '13 at 12:44
  • @user447503 That's incredible. We are talking about milliseconds. Trust me, no-one will notice. You are worrying about the wrong things. – john Apr 06 '13 at 12:46
  • @user447503 BTW the second option, the one you seem to prefer also calls two constructors because it calls the copy constructor as well as one of the other constructors. – john Apr 06 '13 at 12:48
  • The first one won't work if KUrl is not assignable. The second one won't work if KUrl is not copy-constructable. – snak Apr 06 '13 at 12:49
  • @snak I need to check that – basin Apr 06 '13 at 12:55
  • @snak a copy constructor isn't used to init a variable from a different type – basin Apr 06 '13 at 13:27
  • Sure, but in the second code, it constructs an object with one of the constructors and then copy-constructs the final object with it. You can test this by making the copy constructor private. – snak Apr 06 '13 at 14:29
  • There are occasions this won't work, or you might not want to do it this way but I think it counts as the most usual solution. This is why I phrased it as a question. – Jack Aidley Apr 06 '13 at 18:27
  • Stupid gcc. It doesn't really use the copy constructor, but complains if it's private – basin Apr 06 '13 at 19:03
  • 1
    According to the specification, all compilers must check whether it can access a copy constructor even though calling it can be optimized out. – snak Apr 07 '13 at 06:11
1

The usual C++ constructs intentionally create a very tight coupling between allocation and initialization. Thus, ordinarily you would need to use dynamic allocation to be able to dynamically specify the constructor to use. And dynamic allocation is probably some orders of magnitude more inefficient than the slight overhead that you're trying to avoid…

However, with C++11 you can use aligned storage and placement new.

The catch is that the KUrl class will most likely use dynamic allocation internally, and then all that the optimization accomplishes is to waste programmer's time: both your time initially, and the time of anyone later maintaining the code.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
0

Here you have no copy of KUrl being done

boost::optional<KUrl> ourl;
if(customLocation.isEmpty()) {
  ourl = boost::in_place();
} else {
  ourl = boost::in_place(customLocation);
}

KUrl &url = *ourl;

Fun aside, I would recommend Jacks solutions (if it works with your type) :)

Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
  • to avoid dependency on Boost, just use a `std::vector` with `emplace_back`, and just as the above apparently relies on Boost code not doing dynamic allocation, one can then rely on the vector small buffer optimization (hopefully) – Cheers and hth. - Alf Apr 06 '13 at 12:57
  • @Cheersandhth.-Alf vector [may not do](http://stackoverflow.com/questions/8190950/may-stdvector-make-use-of-small-buffer-optimization) small buffer optimization. boost::optional documents that it does the small buffer optimization. – Johannes Schaub - litb Apr 06 '13 at 13:41
0

Any reason why this won't work?

KUrl url;
if (!customLocation.isEmpty())
    url = customLocation;
john
  • 85,011
  • 4
  • 57
  • 81
0
KUrl url;

if (!cusomLocation.isEmpty())
{
    url = KUrl( customLocation );
}
David G
  • 94,763
  • 41
  • 167
  • 253