PortabilityTest.cpp 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /*
  2. * Copyright 2012-present Facebook, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <folly/Portability.h>
  17. #if FOLLY_HAS_STRING_VIEW
  18. #include <string_view> // @manual
  19. #endif
  20. #include <memory>
  21. #include <folly/portability/GTest.h>
  22. class Base {
  23. public:
  24. virtual ~Base() {}
  25. virtual int foo() const {
  26. return 1;
  27. }
  28. };
  29. class Derived : public Base {
  30. public:
  31. int foo() const final {
  32. return 2;
  33. }
  34. };
  35. // A compiler that supports final will likely inline the call to p->foo()
  36. // in fooDerived (but not in fooBase) as it knows that Derived::foo() can
  37. // no longer be overridden.
  38. int fooBase(const Base* p) {
  39. return p->foo() + 1;
  40. }
  41. int fooDerived(const Derived* p) {
  42. return p->foo() + 1;
  43. }
  44. TEST(Portability, Final) {
  45. std::unique_ptr<Derived> p(new Derived);
  46. EXPECT_EQ(3, fooBase(p.get()));
  47. EXPECT_EQ(3, fooDerived(p.get()));
  48. }