隐式转换在模板类中不起作用

时间:2018-04-15 22:44:38

标签: c++ templates implicit-conversion

我有以下类,当我运行测试代码来测试operator /时,编译器会告诉我以下内容(指的是在除法运算符函数中进行的比较)

C2678: binary '==': no operator found which takes a left-hand operand of type 'const foo<8>' (or there is no acceptable conversion)

template<unsigned int t>
class foo {
  foo(int i) {
    // implicit conversion
  }

  template<unsigned int u>
  bool operator==(const foo<u>& obj) const {
    // do some comparison here
  }

  template<unsigned int u>
  foo operator/(const foo<u>& divisor) const {
    if (divisor == 0)
    // perform division
    // return foo result
  }
};

但是,当我将以下代码添加到类中时,没有错误:

friend bool operator==(const foo<t>& lhs, const foo<t>& rhs) {
  // do some comparison here
}

我的测试代码如下:

int main() {
  foo<8> foo_a(0);
  foo<4> foo_b(123);
  foo_b / foo_a;
  return 0;
}

2 个答案:

答案 0 :(得分:1)

问题是在

中找到==
if (divisor == 0)

如果你只有

template<unsigned int u>
bool operator==(const foo<u>& obj) const {
  // do some comparison here
}

编译器无法确定u的任何值是否会为foo<u>提供可从0转换的friend bool operator==(const foo<t>& lhs, const foo<t>& rhs) { // do some comparison here } 。所以它没有使用那个运算符。

另一方面,如果你有

lhs

可以看出,如果foo<t>rhs,那么foo(int i)也必须是foo<t>。然后,它可以使用构造函数0从值==构造elasticsearch (1.0.12) elasticsearch-api (= 1.0.12) elasticsearch-transport (= 1.0.12) elasticsearch-api (1.0.12) multi_json elasticsearch-model (0.1.7) activesupport (> 3) elasticsearch (> 0.4) hashie elasticsearch-rails (0.1.7) elasticsearch-transport (1.0.12) 。现在它具有 GIT specs: activeadmin (1.0.0.pre4) arbre (~> 1.0, >= 1.0.2) bourbon coffee-rails formtastic (~> 3.1) formtastic_i18n inherited_resources (~> 1.6) jquery-rails jquery-ui-rails kaminari (~> 0.15) railties (>= 3.2, < 5.1) ransack (~> 1.3) sass-rails sprockets (< 4.1) GEM specs: CFPropertyList (2.3.1) actionmailer (4.2.7.1) actionpack (= 4.2.7.1) actionview (= 4.2.7.1) activejob (= 4.2.7.1) mail (~> 2.5, >= 2.5.4) rails-dom-testing (~> 1.0, >= 1.0.5) actionpack (4.2.7.1) actionview (= 4.2.7.1) activesupport (= 4.2.7.1) rack (~> 1.6) rack-test (~> 0.6.2) rails-dom-testing (~> 1.0, >= 1.0.5) rails-html-sanitizer (~> 1.0, >= 1.0.2) actionview (4.2.7.1) activesupport (= 4.2.7.1) builder (~> 3.1) erubis (~> 2.7.0) rails-dom-testing (~> 1.0, >= 1.0.5) rails-html-sanitizer (~> 1.0, >= 1.0.2) active_link_to (1.0.2) actionpack active_model_serializers (0.10.2) actionpack (>= 4.1, < 6) activemodel (>= 4.1, < 6) jsonapi (~> 0.1.1.beta2) railties (>= 4.1, < 6) activejob (4.2.7.1) activesupport (= 4.2.7.1) globalid (>= 0.3.0) activemodel (4.2.7.1) activesupport (= 4.2.7.1) builder (~> 3.1) activerecord (4.2.7.1) activemodel (= 4.2.7.1) activesupport (= 4.2.7.1) arel (~> 6.0) activerecord-postgres-earthdistance (0.4.2) pg rails (>= 3.1) rake activesupport (4.2.7.1) i18n (~> 0.7) json (~> 1.7, >= 1.7.7) minitest (~> 5.1) thread_safe (~> 0.3, >= 0.3.4) tzinfo (~> 1.1) addressable (2.3.8) arbre (1.1.1) activesupport (>= 3.0.0) arel (6.0.3) ast (2.3.0) autoprefixer-rails (6.4.1) execjs awesome_print (1.6.1) babel-source (5.8.35) babel-transpiler (0.7.0) babel-source (>= 4.0, < 6) execjs (~> 2.0) bcrypt (3.1.10) binding_of_caller (0.7.2) debug_inspector (>= 0.0.1) bourbon (4.2.7) sass (~> 3.4) thor (~> 0.19) brakeman (3.4.1) breadcrumbs_on_rails (3.0.1) builder (3.2.2) bullet (4.14.4) activesupport (>= 3.0.0) uniform_notifier (>= 1.6.0) bundler-audit (0.3.1) bundler (~> 1.2) thor (~> 0.18) byebug (9.0.6) capybara (2.7.1) addressable mime-types (>= 1.16) nokogiri (>= 1.3.3) rack (>= 1.0.0) rack-test (>= 0.5.4) xpath (~> 2.0) capybara-webkit (1.11.1) capybara (>= 2.3.0, < 2.8.0) json carrierwave (0.10.0) activemodel (>= 3.2.0) activesupport (>= 3.2.0) json (>= 1.7) mime-types (>= 1.16) choice (0.2.0) chronic (0.10.2) code_analyzer (0.4.5) sexp_processor codeclimate-test-reporter (0.4.7) simplecov (>= 0.7.1, < 1.0.0) coderay (1.1.0) coffee-rails (4.2.1) coffee-script (>= 2.2.0) railties (>= 4.0.0, < 5.2.x) coffee-script (2.4.1) coffee-script-source execjs coffee-script-source (1.11.1) colored (1.2) concurrent-ruby (1.0.2) connection_pool (2.2.0) countries (2.0.2) i18n_data (~> 0.7.0) money (~> 6.7) unicode_utils (~> 1.4) crack (0.4.2) safe_yaml (~> 1.0.0) database_cleaner (1.4.1) debug_inspector (0.0.2) decent_exposure (2.3.2) deep_merge (1.1.1) devise (3.5.6) bcrypt (~> 3.0) orm_adapter (~> 0.1) railties (>= 3.2.6, < 5) responders thread_safe (~> 0.1) warden (~> 1.2.3) diff-lcs (1.2.5) docile (1.1.5) domain_name (0.5.24) unf (>= 0.0.5, < 1.0.0) dotenv (2.0.0) dotenv-rails (2.0.0) dotenv (= 2.0.0) elasticsearch (1.0.12) elasticsearch-api (= 1.0.12) elasticsearch-transport (= 1.0.12) elasticsearch-api (1.0.12) multi_json elasticsearch-model (0.1.7) activesupport (> 3) elasticsearch (> 0.4) hashie elasticsearch-rails (0.1.7) elasticsearch-transport (1.0.12) faraday multi_json email_spec (1.6.0) launchy (~> 2.1) mail (~> 2.2) erubis (2.7.0) excon (0.45.3) execjs (2.7.0) factory_girl (4.5.0) activesupport (>= 3.0.0) factory_girl_rails (4.5.0) factory_girl (~> 4.5.0) railties (>= 3.0.0) faker (1.6.6) i18n (~> 0.5) faraday (0.9.1) multipart-post (>= 1.2, < 3) fast_stack (0.1.0) rake rake-compiler fission (0.5.0) CFPropertyList (~> 2.2) flag-icons-rails (1.1.2) sass (~> 3.2) flamegraph (0.1.0) fast_stack fog (1.29.0) fog-atmos fog-aws (~> 0.0) fog-brightbox (~> 0.4) fog-core (~> 1.27, >= 1.27.4) fog-ecloud fog-json fog-local fog-powerdns (>= 0.1.1) fog-profitbricks fog-radosgw (>= 0.0.2) fog-riakcs fog-sakuracloud (>= 0.0.4) fog-serverlove fog-softlayer fog-storm_on_demand fog-terremark fog-vmfusion fog-voxel fog-xml (~> 0.1.1) ipaddress (~> 0.5) nokogiri (~> 1.5, >= 1.5.11) fog-atmos (0.1.0) fog-core fog-xml fog-aws (0.1.2) fog-core (~> 1.27) fog-json (~> 1.0) fog-xml (~> 0.1) ipaddress (~> 0.8) fog-brightbox (0.7.1) fog-core (~> 1.22) fog-json inflecto (~> 0.0.2) fog-core (1.30.0) builder excon (~> 0.45) formatador (~> 0.2) mime-types net-scp (~> 1.1) net-ssh (>= 2.1.3) fog-ecloud (0.1.1) fog-core fog-xml fog-json (1.0.1) fog-core (~> 1.0) multi_json (~> 1.0) fog-local (0.2.1) fog-core (~> 1.27) fog-powerdns (0.1.1) fog-core (~> 1.27) fog-json (~> 1.0) fog-xml (~> 0.1) fog-profitbricks (0.0.2) fog-core fog-xml nokogiri fog-radosgw (0.0.4) fog-core (>= 1.21.0) fog-json fog-xml (>= 0.0.1) fog-riakcs (0.1.0) fog-core fog-json fog-xml fog-sakuracloud (1.0.1) fog-core fog-json fog-serverlove (0.1.2) fog-core fog-json fog-softlayer (0.4.5) fog-core fog-json fog-storm_on_demand (0.1.1) fog-core fog-json fog-terremark (0.1.0) fog-core fog-xml fog-vmfusion (0.1.0) fission fog-core fog-voxel (0.1.0) fog-core fog-xml fog-xml (0.1.2) fog-core nokogiri (~> 1.5, >= 1.5.11) foreman (0.82.0) thor (~> 0.19.1) formatador (0.2.5) formtastic (3.1.4) actionpack (>= 3.2.13) formtastic_i18n (0.6.0) formulaic (0.2.0) activesupport capybara i18n foundation-icons-sass-rails (3.0.0) railties (>= 3.1.1) sass-rails (>= 3.1.1) foundation-rails (5.5.1.1) railties (>= 3.1.0) sass (>= 3.3.0, < 3.5) friendly_id (5.1.0) activerecord (>= 4.0.0) fuubar (2.0.0) rspec (~> 3.0) ruby-progressbar (~> 1.4) globalid (0.3.7) activesupport (>= 4.1.0) has_scope (0.6.0) actionpack (>= 3.2, < 5) activesupport (>= 3.2, < 5) hashie (3.4.1) heroku (3.32.0) heroku-api (>= 0.3.19) launchy (>= 0.3.2) multi_json (>= 1.10) netrc (>= 0.10.0) rest-client (>= 1.6.0) rubyzip (>= 0.9.9) heroku-api (0.3.23) excon (~> 0.44) multi_json (~> 1.8) http-cookie (1.0.2) domain_name (~> 0.5) i18n (0.7.0) i18n_data (0.7.0) inflecto (0.0.2) inherited_resources (1.6.0) actionpack (>= 3.2, < 5) has_scope (~> 0.6.0.rc) railties (>= 3.2, < 5) responders inline_svg (0.11.0) activesupport (>= 4.0) loofah (>= 2.0) nokogiri (~> 1.6) interactor (3.1.0) ipaddress (0.8.0) jquery-rails (4.0.4) rails-dom-testing (~> 1.0) railties (>= 4.2.0) thor (>= 0.14, < 2.0) jquery-ui-rails (5.0.5) railties (>= 3.2.16) json (1.8.3) jsonapi (0.1.1.beta2) json (~> 1.8) jwt (1.5.0) kaminari (0.17.0) actionpack (>= 3.0.0) activesupport (>= 3.0.0) launchy (2.4.3) addressable (~> 2.3) letter_opener (1.3.0) launchy (~> 2.2) libv8 (3.16.14.15) loofah (2.0.3) nokogiri (>= 1.5.9) mail (2.6.4) mime-types (>= 1.16, < 4) masonry-rails (0.2.4) railties memory_profiler (0.9.4) metamagic (3.1.7) rails (>= 3.0.0) method_source (0.8.2) mime-types (2.99.3) mini_magick (4.2.4) mini_portile2 (2.1.0) minitest (5.10.1) money (6.7.1) i18n (>= 0.6.4, <= 0.7.0) sixarm_ruby_unaccent (>= 1.1.1, < 2) multi_json (1.12.1) multi_xml (0.5.5) multipart-post (2.0.0) mysql (2.9.1) net-scp (1.2.1) net-ssh (>= 2.6.5) net-ssh (2.9.2) netrc (0.10.3) newrelic_rpm (3.12.1.298) nokogiri (1.6.8.1) mini_portile2 (~> 2.1.0) oauth2 (1.0.0) faraday (>= 0.8, < 0.10) jwt (~> 1.0) multi_json (~> 1.3) multi_xml (~> 0.5) rack (~> 1.2) oj (2.18.0) omniauth (1.2.2) hashie (>= 1.2, < 4) rack (~> 1.0) omniauth-facebook (2.0.1) omniauth-oauth2 (~> 1.2) omniauth-google-oauth2 (0.2.4) omniauth (~> 1.0) omniauth-oauth2 (~> 1.1) omniauth-oauth2 (1.3.0) oauth2 (~> 1.0) omniauth (~> 1.2) orm_adapter (0.5.0) parser (2.3.3.1) ast (~> 2.2) pg (0.18.1) pgbackups-archive (1.0.3) fog-aws heroku (>= 3.28.6, <= 3.32) rake polyamorous (1.3.1) activerecord (>= 3.0) powerpack (0.1.1) pry (0.10.1) coderay (~> 1.1.0) method_source (~> 0.8.1) slop (~> 3.4) pry-rails (0.3.4) pry (>= 0.9.10) puma (3.6.2) pundit (1.1.0) activesupport (>= 3.0.0) quiet_assets (1.1.0) railties (>= 3.1, < 5.0) rabl (0.13.1) activesupport (>= 2.3.14) rack (1.6.5) rack-canonical-host (0.1.0) addressable rack (~> 1.0) rack-cors (0.4.0) rack-mini-profiler (0.10.1) rack (>= 1.2.0) rack-test (0.6.3) rack (>= 1.0) rails (4.2.7.1) actionmailer (= 4.2.7.1) actionpack (= 4.2.7.1) actionview (= 4.2.7.1) activejob (= 4.2.7.1) activemodel (= 4.2.7.1) activerecord (= 4.2.7.1) activesupport (= 4.2.7.1) bundler (>= 1.3.0, < 2.0) railties (= 4.2.7.1) sprockets-rails rails-assets-autosize (3.0.17) rails-assets-dropzone (4.3.0) rails-assets-jquery (2.1.4) rails-assets-jquery-bar-rating (1.1.0) rails-assets-jquery (>= 1.7.2) rails-assets-jquery-cascading-dropdown (1.2.5) rails-assets-jquery-placeholder (2.1.1) rails-assets-jquery (>= 1.6) rails-assets-jquery-ui (1.11.4) rails-assets-jquery (>= 1.6) rails-assets-lite-uploader (2.2.0) rails-assets-jquery (>= 1.8) rails-assets-lodash (4.14.2) rails-assets-microplugin (0.0.3) rails-assets-pnotify (3.0.0) rails-assets-jquery (>= 1.6) rails-assets-rangeslider.js (1.2.1) rails-assets-jquery (>= 1.9.0) rails-assets-sachinchoolur--lightGallery (1.3.5) rails-assets-jquery (>= 1.7.0) rails-assets-selectize (0.12.3) rails-assets-jquery (>= 1.7.0) rails-assets-microplugin (~> 0.0.0) rails-assets-sifter (~> 0.5.0) rails-assets-sifter (0.5.2) rails-assets-timepicker (1.7.0) rails-assets-jquery (>= 1.7) rails-deprecated_sanitizer (1.0.3) activesupport (>= 4.2.0.alpha) rails-dom-testing (1.0.7) activesupport (>= 4.2.0.beta, < 5.0) nokogiri (~> 1.6.0) rails-deprecated_sanitizer (>= 1.0.1) rails-erd (1.4.0) activerecord (>= 3.2) activesupport (>= 3.2) choice (~> 0.2.0) ruby-graphviz (~> 1.2) rails-html-sanitizer (1.0.3) loofah (~> 2.0) rails_12factor (0.0.3) rails_serve_static_assets rails_stdout_logging rails_best_practices (1.15.7) activesupport code_analyzer (>= 0.4.3) colored erubis i18n json require_all ruby-progressbar rails_serve_static_assets (0.0.4) rails_stdout_logging (0.0.3) railties (4.2.7.1) actionpack (= 4.2.7.1) activesupport (= 4.2.7.1) rake (>= 0.8.7) thor (>= 0.18.1, < 2.0) rainbow (2.1.0) rake (12.0.0) rake-compiler (0.9.5) rake ransack (1.8.2) actionpack (>= 3.0) activerecord (>= 3.0) activesupport (>= 3.0) i18n polyamorous (~> 1.3) react-rails (1.8.1) babel-transpiler (>= 0.7.0) coffee-script-source (~> 1.8) connection_pool execjs railties (>= 3.2) tilt ref (2.0.0) remotipart (1.2.1) require_all (1.3.2) responders (2.3.0) railties (>= 4.2.0, < 5.1) rest-client (1.8.0) http-cookie (>= 1.0.2, < 2.0) mime-types (>= 1.16, < 3.0) netrc (~> 0.7) rinku (1.7.3) rollbar (2.13.3) multi_json rspec (3.5.0) rspec-core (~> 3.5.0) rspec-expectations (~> 3.5.0) rspec-mocks (~> 3.5.0) rspec-core (3.5.4) rspec-support (~> 3.5.0) rspec-expectations (3.5.0) diff-lcs (>= 1.2.0, < 2.0) rspec-support (~> 3.5.0) rspec-mocks (3.5.0) diff-lcs (>= 1.2.0, < 2.0) rspec-support (~> 3.5.0) rspec-rails (3.5.2) actionpack (>= 3.0) activesupport (>= 3.0) railties (>= 3.0) rspec-core (~> 3.5.0) rspec-expectations (~> 3.5.0) rspec-mocks (~> 3.5.0) rspec-support (~> 3.5.0) rspec-support (3.5.0) rubocop (0.46.0) parser (>= 2.3.1.1, < 3.0) powerpack (~> 0.1) rainbow (>= 1.99.1, < 3.0) ruby-progressbar (~> 1.7) unicode-display_width (~> 1.0, >= 1.0.1) ruby-graphviz (1.2.2) ruby-progressbar (1.8.1) rubyzip (1.1.7) safe_yaml (1.0.4) sass (3.4.22) sass-rails (5.0.6) railties (>= 4.0.0, < 6) sass (~> 3.1) sprockets (>= 2.8, < 4.0) sprockets-rails (>= 2.0, < 4.0) tilt (>= 1.1, < 3) scss_lint (0.38.0) rainbow (~> 2.0) sass (~> 3.4.1) seedbank (0.3.0) sexp_processor (4.7.0) shoulda-matchers (2.8.0) activesupport (>= 3.0.0) simple_form (3.1.0) actionpack (~> 4.0) activemodel (~> 4.0) simplecov (0.9.2) docile (~> 1.1.0) multi_json (~> 1.0) simplecov-html (~> 0.9.0) simplecov-html (0.9.0) sitemap_generator (5.2.0) builder (~> 3.0) sixarm_ruby_unaccent (1.1.1) skim (0.10.0) coffee-script coffee-script-source (>= 1.2.0) slim (>= 3.0) sprockets (>= 2, < 4) slim (3.0.3) temple (~> 0.7.3) tilt (>= 1.3.3, < 2.1) slim-rails (3.0.1) actionmailer (>= 3.1, < 5.0) actionpack (>= 3.1, < 5.0) activesupport (>= 3.1, < 5.0) railties (>= 3.1, < 5.0) slim (~> 3.0) slop (3.6.0) sprockets (3.7.0) concurrent-ruby (~> 1.0) rack (> 1, < 3) sprockets-rails (3.2.0) actionpack (>= 4.0) activesupport (>= 4.0) sprockets (>= 3.0.0) stackprof (0.2.7) temple (0.7.5) test_after_commit (0.4.1) activerecord (>= 3.2) therubyracer (0.12.2) libv8 (~> 3.16.14.0) ref thor (0.19.4) thread_safe (0.3.5) tilt (2.0.5) truncate_html (0.9.3) tzinfo (1.2.2) thread_safe (~> 0.1) uglifier (2.7.2) execjs (>= 0.3.0) json (>= 1.8.0) unf (0.1.4) unf_ext unf_ext (0.0.7.1) unicode-display_width (1.1.1) unicode_utils (1.4.0) uniform_notifier (1.8.0) validate_url (1.0.0) activemodel (>= 3.0.0) addressable warden (1.2.6) rack (>= 1.0) web-console (2.1.3) activemodel (>= 4.0) binding_of_caller (>= 0.7.2) railties (>= 4.0) sprockets-rails (>= 2.0, < 4.0) webmock (1.21.0) addressable (>= 2.3.6) crack (>= 0.3.2) xpath (2.0.0) nokogiri (~> 1.3) PLATFORMS ruby DEPENDENCIES active_link_to active_model_serializers activeadmin! activerecord-postgres-earthdistance addressable autoprefixer-rails awesome_print brakeman breadcrumbs_on_rails bullet bundler-audit byebug capybara capybara-webkit carrierwave chronic codeclimate-test-reporter coffee-rails countries database_cleaner decent_exposure deep_merge devise dotenv-rails elasticsearch-model elasticsearch-rails email_spec factory_girl_rails faker flag-icons-rails flamegraph fog fog-aws foreman formulaic foundation-icons-sass-rails foundation-rails friendly_id fuubar inline_svg interactor jquery-rails (= 4.0.4) jquery-ui-rails (= 5.0.5) kaminari launchy letter_opener masonry-rails memory_profiler metamagic mini_magick mysql newrelic_rpm oj omniauth omniauth-facebook omniauth-google-oauth2 pg pgbackups-archive pry-rails puma pundit quiet_assets rabl rack-canonical-host rack-cors rack-mini-profiler rails (= 4.2.7.1) rails-assets-autosize! rails-assets-dropzone! rails-assets-jquery-bar-rating! rails-assets-jquery-cascading-dropdown! rails-assets-jquery-placeholder! rails-assets-jquery-ui! rails-assets-lite-uploader! rails-assets-lodash! rails-assets-pnotify! rails-assets-rangeslider.js! rails-assets-sachinchoolur--lightGallery! rails-assets-selectize! rails-assets-timepicker! rails-erd rails_12factor rails_best_practices react-rails remotipart responders rinku rollbar rspec-rails rubocop sass-rails scss_lint seedbank shoulda-matchers simple_form sitemap_generator skim slim slim-rails stackprof test_after_commit therubyracer truncate_html uglifier validate_url web-console webmock RUBY VERSION ruby 2.3.1p112 BUNDLED WITH 1.13.6 双方的价值。

答案 1 :(得分:0)

向您的类构造函数添加显式,错误将返回。

explicit foo(int i)

编译器会发生什么,请尝试以下代码:

if (divisor == 0)

通过从0创建foo对象(它可以这样做,因为你的构造函数不是显式的),但是失败了,因为比较了两个foo对象,你没有提供这个操作符。

添加功能时:

friend bool operator==(const foo<t>& lhs, const foo<t>& rhs) {
  // do some comparison here
}

您确实提供了必要的操作员。